I'd like to set text with ruby in the iOS app UILabel.attributedText, but even if I substitute text with CFFAtributedStringRef type ruby in the UILabel, the compilation passes but stops at runtime error.
It is part of the code below. http://dev.classmethod.jp/references/ios8-ctrubyannotationref/ was used as a reference.
UILabel*textLbl=(UILabel*) [cell viewWithTag:1];
CFSstringRef writing=(__bridge CFSstringRef)@"Tokyo";
CFSstringRef furigana [kCTRubyPositionCount] = {
(__bridge CFSstringRef)@ "To", NULL, NULL, NULL
};
CTRubyAnnotationRefruby=CTRubyAnnotationCreate (kCTRubyAlignmentAuto, kCTRubyOverhangAuto, 0.5, furigana);
CFAattributedStringRef writingAttributedString= [selfattributedString:writing ruby:ruby];
// It stops when it is called signal sigabrt.
textLbl.attributedText=(__bridge NAttributedString*) (writingAttributedString);
// [self-attributedString:writing ruby:ruby]section
- (CFAtributedStringRef) attributeString: (CFSstringRef) string ruby: (CTRubyAnnotationRef) ruby
{
// Font style
CTFontRefont=CTFontCreateWithName(CFSTR("Verdana"), 28, NULL);
// Font color
CGColorRefontColor=[UIColor redColor].CGColor;
// paragraph
CTTextAlignment alignment=kCTRightTextAlignment;
CTParographStyleSetting settings[] = {
{kCTParographStyleSpecifierAlignment, size of (alignment), & alignment}
};
CTParographStyleRef paragrafStyle=CTParagrafStyleCreate(settings,sizeof(settings)/sizeof(settings[0]));
// Create an attribute string
CFSstringRef keys[] = {kCTFontAttributeName, kCTParographStyleAttributeName, kCTForegroundColorAttributeName, kCTRubyAnnotationAttributeName};
CFTypeRef values[] = {font, paragraphStyle, fontColor, ruby};
CFDictionaryRefattr = CFDictionaryCreate(NULL, (const void**)&keys, (const void**)&values,
sizeof(keys)/sizeof(keys[0]), &kCFTyDictionaryKeyCallBacks, &kCFTyDictionaryValueCallBacks);
CFAtributedStringRefattributes = CFAtributedStringCreate (NULL, string, attr);
CFRelease (attr);
return attributes;
}
Is there any other way to configure ruby text in UILabel?
ios objective-c uilabel
It's Swift3.
Most of the processing is done using Qiita's post below.Some of them have been rewritten to Swift3.
Shake Rubi by @woxtuon @Qiita
http://qiita.com/woxtu/items/284369fd2654edac2248
Simply set the NNSAttributedString to the UILabel attributeText
class ViewController:UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad(){
super.viewDidLoad()
Let text="|Tokyo Metropolitan Government"
label.attributedText=text.attributedStringWithRuby()
}
}
extension String {
US>funcatedAttributedStringWithRuby()->NSMutableAttributedString{
// "|"—Symbol (full-width) used to determine and separate the target string of the rubi wave. Insert at the beginning of the string of the rubi wave
// // "《》": ルビを振る対象の漢字の直後に挿入しルビを囲う(全角)
let-attributed=
self.replace(pattern:"(|.+? ..+?)), template:", $1,")
.components(separatedBy:",")
.map {x->NSAttributedString in
iflet pair=x.find(pattern:"|(.+?)"){
let string=(x as NSSstring).substring(with:pair.rangeAt(1)))
let ruby=(x as NSSstring).substring(with:pair.rangeAt(2))
var text = [.passRetained (ruby as CFSstring) as Unmanaged <CFSstring>?, .none, .none, .none]
letnotation=CTRubyAnnotationCreate (.auto, .auto, 0.5, & text[0]!)
US>return NNSAttributedString(
string —string,
attributes: [kCTRubyAnnotationAttributeName as String: announcement])
} else{
return NNSAttributedString (string:x, attributes:nil)
}
}
.reduce(NSMutableAttributedString()) {$0.append($1);return$0}
returned attributed
}
func find(pattern:String)->NSTextCheckingResult? {
do{
letre=try NSRegularExpression (pattern:pattern, options:[])
return.firstMatch(
in —self,
options: [ ],
range —NSMakeRange(0,self.utf16.count))
} catch{
return nil
}
}
func replace(pattern:String, template:String) - > String {
do{
letre=try NSRegularExpression (pattern:pattern, options:[])
return.stringByReplacingMatches(
in —self,
options: [ ],
range —NSMakeRange(0,self.utf16.count),
withTemplate:template)
} catch{
return self
}
}
}
© 2024 OneMinuteCode. All rights reserved.