在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: "")
答案 0 :(得分:7)
在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];
使用它会自动加粗从第二个索引开始然后是4个字符的字符。 例如: 1234567890 - 12 3456 7890
希望这有帮助。
对于SWIFT语言:
let font:UIFont? = UIFont.boldSystemFontOfSize(12.0)
myMutableString.addAttribute(NSFontAttributeName, value: font!, range: NSRange(location: 2, length: 4));
答案 1 :(得分:4)
感谢Martins之前的回答,我编辑了他的解决方案,以配合我的案例,它完美无瑕 检查并提出他的解决方案:create an attributed string out of plain (Android formated) text in swift for iOS
所以这基本上改变了:
<a>hey</a> to size 14 bold
<b>hey</b> to size 12 bold
<u>hey</u> to underlined
很容易添加更多功能。
//localizable.strings
"rulesText" = "\n\n<a>The following will be removed</a> \n\n<b><u>Harassment</u></b>\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: "<b>", propsEndIndicator: "</b>")
attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldBigFont!, propsIndicator: "<a>", propsEndIndicator: "</a>")
attrString = fixText(attrString, attributeName: NSUnderlineStyleAttributeName, attributeValue: NSUnderlineStyle.StyleDouble.rawValue, propsIndicator: "<u>", propsEndIndicator: "</u>")
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
}
答案 2 :(得分:0)
//上述方法的目标C格式回答为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:@"<b>" adnpropsEndIndicator:@"</b>"];
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;
}