Ios 在UILabel中显示日语Furigana
Furigana(也称为rubi)是日语阅读辅助工具,由较小的假名或音节字符组成,印在汉字(表意字符)旁边 我在网上找到了如何用以下代码显示furiganaIos 在UILabel中显示日语Furigana,ios,swift,uilabel,cjk,Ios,Swift,Uilabel,Cjk,Furigana(也称为rubi)是日语阅读辅助工具,由较小的假名或音节字符组成,印在汉字(表意字符)旁边 我在网上找到了如何用以下代码显示furigana class ViewController: UIViewController { @IBOutlet weak var label: UILabel! override func viewDidLoad() { super.viewDidLoad() let text = "|東京都《とうきょうと》" label
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let text = "|東京都《とうきょうと》"
label.attributedText = text.attributedStringWithRuby()
}
extension String {
func attributedStringWithRuby() -> NSMutableAttributedString {
// "|": ルビを振る対象の文字列を判定し区切る為の記号(全角). ルビを振る文字列の先頭に挿入する
// "《》": ルビを振る対象の漢字の直後に挿入しルビを囲う(全角)
let attributed =
self.replace(pattern: "(|.+?《.+?》)", template: ",$1,")
.components(separatedBy: ",")
.map { x -> NSAttributedString in
if let pair = x.find(pattern: "|(.+?)《(.+?)》") {
let string = (x as NSString).substring(with: pair.rangeAt(1))
let ruby = (x as NSString).substring(with: pair.rangeAt(2))
var text = [.passRetained(ruby as CFString) as Unmanaged<CFString>?, .none, .none, .none]
let annotation = CTRubyAnnotationCreate(.auto, .auto, 0.5, &text[0]!)
return NSAttributedString(
string: string,
attributes: [kCTRubyAnnotationAttributeName as String: annotation])
} else {
return NSAttributedString(string: x, attributes: nil)
}
}
.reduce(NSMutableAttributedString()) { $0.append($1); return $0 }
return attributed
}
func find(pattern: String) -> NSTextCheckingResult? {
do {
let re = try NSRegularExpression(pattern: pattern, options: [])
return re.firstMatch(
in: self,
options: [],
range: NSMakeRange(0, self.utf16.count))
} catch {
return nil
}
}
func replace(pattern: String, template: String) -> String {
do {
let re = try NSRegularExpression(pattern: pattern, options: [])
return re.stringByReplacingMatches(
in: self,
options: [],
range: NSMakeRange(0, self.utf16.count),
withTemplate: template)
} catch {
return self
}
}
类ViewController:UIViewController{
@IBVAR标签:UILabel!
重写func viewDidLoad(){
super.viewDidLoad()
让text=“”)東京都《とうきょうと》"
label.attributedText=text.attributedStringWithRuby()
}
扩展字符串{
func attributedStringWithRuby()->NSMutableAttributedString{
// "|": ルビを振る対象の文字列を判定し区切る為の記号(全角). ルビを振る文字列の先頭に挿入する
// "《》": ルビを振る対象の漢字の直後に挿入しルビを囲う(全角)
让与=
self.replace(模式:“(\+?”),模板:“,$1”)
.组件(以“,”分隔)
.map{x->nsattributed字符串位于
如果let pair=x.find(模式:“\(+?)”>){
让string=(x为NSString).substring(带:pair.rangeAt(1))
让ruby=(x为NSString).substring(带:pair.rangeAt(2))
var text=[.passRetained(ruby作为CFString)作为非托管?,.none,.none,.none]
let annotation=CTRubyAnnotationCreate(.auto、.auto、0.5和text[0]!)
返回NSAttribute字符串(
弦:弦,
属性:[kCTRubyAnnotationAttributeName作为字符串:注释])
}否则{
返回NSAttributedString(字符串:x,属性:nil)
}
}
.reduce(nsmutableAttributeString()){$0.append($1);返回$0}
返回属性
}
func find(模式:String)->NSTextCheckingResult{
做{
让re=尝试NSRegularExpression(模式:模式,选项:[])
返回第一场比赛(
在:self,,
选项:[],
范围:NSMakeRange(0,self.utf16.count))
}抓住{
归零
}
}
func replace(模式:字符串,模板:字符串)->String{
做{
让re=尝试NSRegularExpression(模式:模式,选项:[])
返回re.stringByReplacingMatches(
在:self,,
选项:[],
范围:NSMakeRange(0,self.utf16.count),
withTemplate:template)
}抓住{
回归自我
}
}
我是Swift新手,所以我只是复制并粘贴到我的x代码项目中,但它不起作用(错误:无法将“Unmanaged”类型的值转换为预期的参数类型“UnsafemeutablePointer?”>)
我尽可能多地研究,但都没用。请帮忙
结果应该是这样的
不是完整的答案,但这里是我在Swift 4中使用ruby的代码中的一个快速片段-这可以清理,但可能是一个有价值的代码示例,因为很难找到这些函数的资源:
let annotationAttribs: [AnyHashable: Any] = [
kCTRubyAnnotationSizeFactorAttributeName: rubySizeFactor,
kCTRubyAnnotationScaleToFitAttributeName: true,
]
let annotation = CTRubyAnnotationCreateWithAttributes(
alignment, .auto, CTRubyPosition.before, ruby as CFString, annotationAttribs as CFDictionary)
let attribs: [NSAttributedStringKey: Any] = [
NSAttributedStringKey(kCTRubyAnnotationAttributeName as String): annotation, //.takeUnretainedValue(),
NSAttributedStringKey("RubyText"): ruby,
]
return NSAttributedString(
string: string,
attributes: attribs)
这不是一个完整的答案,但这里是我在Swift 4中使用ruby的代码的一个快速片段-这可以被清理,但可能是一个有价值的代码示例,因为很难找到这些函数的资源:
let annotationAttribs: [AnyHashable: Any] = [
kCTRubyAnnotationSizeFactorAttributeName: rubySizeFactor,
kCTRubyAnnotationScaleToFitAttributeName: true,
]
let annotation = CTRubyAnnotationCreateWithAttributes(
alignment, .auto, CTRubyPosition.before, ruby as CFString, annotationAttribs as CFDictionary)
let attribs: [NSAttributedStringKey: Any] = [
NSAttributedStringKey(kCTRubyAnnotationAttributeName as String): annotation, //.takeUnretainedValue(),
NSAttributedStringKey("RubyText"): ruby,
]
return NSAttributedString(
string: string,
attributes: attribs)
请,你能发布一个使用此代码的示例吗?不幸的是,我不太适合应用此代码抱歉,目前没有完整的示例,但是如果你四处搜索,你可以找到需要修改的内容。请,你能发布一个使用此代码的示例吗?不幸的是,我不太适合应用此代码抱歉,没有完整的示例目前为您提供样本,但如果您四处搜索,您可以找到适合的内容。