是否有一种unicode方法可以使字符串的一部分变为粗体?

在Localizable.strings中

"rulesText" = "No illegal posting! \n No weird stuff! \n There is no tolerance for objectionable content, they will be removed!"; 

我可以参与这个大胆的活动吗? 就像没有奇怪的东西! 或者在这个意义上使用unicode字符的东西? 或者其他一些方式?

我这样使用它:

 textView.text = "\n\n " + NSLocalizedString("rulesText", comment: "") 

在textView.text中使用时,更简单的方法是使用NSMutableAttributedString 。 这是一个例子:

 NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString: NSLocalizedString("rulesText", comment: "")]; [attrString beginEditing]; [attrString addAttribute:kCTFontAttributeName value:[[UIFont boldSystemFontOfSize:12] fontName] range:NSMakeRange(2, 4)]; // use range as per your character index range [attrString endEditing]; 

使用它会自动加粗从第2个索引开始然后是4个字符的字符。 例如:1234567890 – 12 3456 7890

希望这可以帮助。

对于SWIFT语言:

 let font:UIFont? = UIFont.boldSystemFontOfSize(12.0) myMutableString.addAttribute(NSFontAttributeName, value: font!, range: NSRange(location: 2, length: 4)); 

http://www.raywenderlich.com/77092/text-kit-tutorial-swift

感谢Martins之前的回答,我编辑了他的解决方案以匹配我的案例,它完美无缺。
检查并提出他的解决方案: 使用swift for iOS中的普通(Android格式化)文本创建一个属性字符串

所以这基本上改变了:

 hey to size 14 bold hey to size 12 bold hey to underlined 

它很容易添加更多function。

 //localizable.strings "rulesText" = "\n\nThe following will be removed \n\nHarassment\n\nOther Stuff" //viewdidload textView.font = UIFont(name: "HelveticaNeue-Light", size: 12) //This is here to set up rest of the texts font textView.attributedText = convertText(NSLocalizedString("rulesText", comment: "")) //method for string conversation func convertText(inputText: String) -> NSAttributedString { var attrString = NSMutableAttributedString(string: inputText) let boldFont = UIFont(name: "Helvetica-Bold", size: 12) let boldBigFont = UIFont(name: "Helvetica-Bold", size: 14) attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldFont!, propsIndicator: "", propsEndIndicator: "") attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldBigFont!, propsIndicator: "", propsEndIndicator: "") attrString = fixText(attrString, attributeName: NSUnderlineStyleAttributeName, attributeValue: NSUnderlineStyle.StyleDouble.rawValue, propsIndicator: "", propsEndIndicator: "") return attrString } func fixText(inputText:NSMutableAttributedString, attributeName:AnyObject, attributeValue:AnyObject, propsIndicator:String, propsEndIndicator:String)->NSMutableAttributedString{ var r1 = (inputText.string as NSString).rangeOfString(propsIndicator) while r1.location != NSNotFound { let r2 = (inputText.string as NSString).rangeOfString(propsEndIndicator) if r2.location != NSNotFound && r2.location > r1.location { let r3 = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length) inputText.addAttribute(attributeName as String, value: attributeValue, range: r3) inputText.replaceCharactersInRange(r2, withString: "") inputText.replaceCharactersInRange(r1, withString: "") } else { break } r1 = (inputText.string as NSString).rangeOfString(propsIndicator) } return inputText } 

//目标C格式的方法上面的Esq回答了为NSLocalizedString创建属性字符串

 -(NSAttributedString* )convertText:(NSString*)inputText { NSMutableAttributedString * attString = [[NSMutableAttributedString alloc] initWithString:inputText]; UIFont *makeBold = [UIFont boldSystemFontOfSize:16]; attString = [self fixText:attString andFont:makeBold andAttributeName:NSFontAttributeName andProsPoseIndicator:@"" adnpropsEndIndicator:@""]; return attString; } -(NSMutableAttributedString *)fixText:(NSMutableAttributedString *) inputText andFont:(UIFont*)attributeValue andAttributeName: (NSString *)attributeName andProsPoseIndicator: (NSString *)propsIndicator adnpropsEndIndicator: (NSString *)propsEndIndicator{ NSRange r = [inputText.string rangeOfString:propsIndicator]; while (r.location != NSNotFound) { NSRange r2 = [inputText.string rangeOfString:propsEndIndicator]; if (r.location != NSNotFound && r2.location >r.location) { NSRange r3 = NSMakeRange(r.location+r2.length-1, r2.location - r.location - r.length); [inputText addAttribute:attributeName value:attributeValue range:r3]; [inputText replaceCharactersInRange:r2 withString:@""]; [inputText replaceCharactersInRange:r withString:@""]; }else { break; } r = [inputText.string rangeOfString:(propsIndicator)]; } return inputText; } 

Swift 3中的工作版本。适用于Label和TextView。

  @IBOutlet var theLabel: UILabel! @IBOutlet var theTextview: UITextView! override func viewDidLoad() { super.viewDidLoad() let theString = "Please satisfy three of the following password conditions" as NSString let theAttributedString = NSMutableAttributedString(string: theString as String) let boldString = "three" let boldRange = theString.range(of: boldString) let font = UIFont.boldSystemFont(ofSize: 20) theAttributedString.addAttribute(NSFontAttributeName, value: font, range: boldRange) theLabel.attributedText = theAttributedString theTextview.attributedText = theAttributedString } 

(从3.1开始,转换Range的问题很少,因为’addAttribute’中的’range’参数仍然是NSRange而不是Range。这可以通过将String类型转换为NSString并在NSRange中获取范围’boldRange’来解决

对于Swift 4 ,只需更改此行即可

 theAttributedString.addAttribute(NSAttributedStringKey.font, value: font, range: boldRange)