Ios 为什么可以';我不能用NSMutableString在Swift中只加粗字符串的一部分吗?
问题是我可以给一半的字符串上色,但我不能给那一半的字符串加粗 这是密码Ios 为什么可以';我不能用NSMutableString在Swift中只加粗字符串的一部分吗?,ios,swift,Ios,Swift,问题是我可以给一半的字符串上色,但我不能给那一半的字符串加粗 这是密码 var string = "blah blah blah blah blah" var range = NSRange(location:0, length: 4) //Apply to the label myMutableString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont
var string = "blah blah blah blah blah"
var range = NSRange(location:0, length: 4)
//Apply to the label
myMutableString.addAttribute(NSFontAttributeName,
value: UIFont.boldSystemFontOfSize([X size]), range: range)
myMutableString.addAttribute(NSForegroundColorAttributeName,
value: UIColor.green(),
range: range)
UILabel.text.attributedText = myMutableString
你知道为什么粗体字会将整个字符串加粗,而不仅仅是第一个“废话”吗?只有第一个“废话”是绿色的。我只是发布了一个答案,因为以交互方式使用操场并实时查看UILabel的变化很有趣
import UIKit
let label = UILabel(frame: CGRectMake(0, 0, 300, 100))
let text = "black green small"
var mutableString = NSMutableAttributedString(string: text)
let textCount = text.utf16.count // Thanks OOPer
mutableString.addAttribute(NSFontAttributeName,
value: UIFont.boldSystemFontOfSize(20),
range: NSRange(location:0, length: textCount*2/3)
)
mutableString.addAttribute(NSForegroundColorAttributeName,
value: UIColor.greenColor(),
range: NSRange(textCount/3..<textCount)
)
label.attributedText = mutableString
label
导入UIKit
let label=UILabel(帧:CGRectMake(0,0,300,100))
让text=“黑绿色小”
var mutableString=nsmutableAttributeString(字符串:文本)
让textCount=text.utf16.count//
mutableString.addAttribute(NSFontAttributeName,
值:UIFont.boldSystemFontOfSize(20),
范围:NSRange(位置:0,长度:textCount*2/3)
)
mutableString.addAttribute(NSForegroundColorAttributeName,
值:UIColor.greenColor(),
范围:NSRange(textCount/3..String.CharacterView.Index{
让amountOfCharacters=Int(浮点(self.characters.count)*分数)
返回self.characters.startIndex.advancedBy(amountOfCharacters,限制:self.characters.endIndex)
}
func utf16Index(characterIndex索引:String.CharacterView.index)->String.UTF16View.index{
返回字符串.UTF16View.Index(索引,在:self.utf16内)
}
func utf16Offset(toIndex索引:String.UTF16View.index)->String.UTF16View.index.Distance{
返回self.utf16.startIndex.distanceTo(索引)
}
}
扩展字符串{//
func utf16Offset(分数分数:Float)->String.UTF16View.Index.Distance{
设correctIndexOfFraction=self.characterIndex(分数:分数)
让translatedtoutp16=self.utf16Index(characterIndex:correctedIndexOffraction)
设absolutePosition=self.utf16偏移量(toIndex:TranslatedTout016)
返回绝对位置
}
}
func paintedString(文本:String)->NSAttributedString{
让mutableString=nsmutableAttributeString(字符串:文本)
让firstTwoTrithers=0..您的代码不工作,因为即使在属性设置为myMutableString之前,字符串也应该被分配给myMutableString
var string = "blah blah blah blah blah"
let myMutableString = NSMutableAttributedString(string: string)
var range = NSRange(location:0, length: 4)
//Apply to the label
myMutableString.addAttribute(NSFontAttributeName,
value: UIFont.boldSystemFontOfSize(10), range: range)
myMutableString.addAttribute(NSForegroundColorAttributeName,
value: UIColor.blueColor(),
range: range)
工作代码基于您的要求:
你是说NSMutableAttributedString吗?试着给出一个完整的工作示例,你的示例使用的是我们无法访问的变量和东西。什么是myMutableString
?它在哪里设置/初始化?NSMutableAttributedString
使用基于UTF-16的计数和偏移量。你应该使用text.utf16.count
ntext.characters.count
。谢谢OOPer,我不知道!(我编辑了我的答案)谢谢你的回答,但你编辑的代码并不完美。因为textCount*2/3
或textCount/3
可能是无效的索引,当你的文本
包含一些非BMP字符时。请查看let text=“\u{1F110}的情况\u{1F111}\u{1F112}\u{1F113}\u{1F114}
。该死的,你太严格了,我又编辑了一遍。顺便说一句,你让我学到了很多东西,谢谢;)太好了!谢谢你的更新。你的方式似乎比我想象的更严格。希望你的努力可以帮助很多开发人员完成它。再次感谢你。
var string = "blah blah blah blah blah"
let myMutableString = NSMutableAttributedString(string: string)
var range = NSRange(location:0, length: 4)
//Apply to the label
myMutableString.addAttribute(NSFontAttributeName,
value: UIFont.boldSystemFontOfSize(10), range: range)
myMutableString.addAttribute(NSForegroundColorAttributeName,
value: UIColor.blueColor(),
range: range)