【问题标题】:Is there a unicode way to make part of a string bold?有没有一种 unicode 方法可以使字符串的一部分变为粗体?
【发布时间】:2017-12-12 18:24:12
【问题描述】:

在 Localizable.strings 中

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

我可以把这个加粗吗?像 No奇怪的东西! 或者在这个意义上使用 unicode 字符的东西?还是其他方式?

我是这样使用的:

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

【问题讨论】:

  • 你在哪里使用“rulesText”在哪个小部件、textview、webview 中?
  • textview.edited 问题
  • Unicode 是字符串的代码表,因为字符不是字符串呈现给用户的方式。在 Unicode 中,有更多字符允许没有 ASCII 中不可用的拉丁字母。因此,您应该了解 Unicode 与它呈现给用户的方式无关。
  • 你可以为它使用属性字符串
  • ... 在这里翻译成 Swift:stackoverflow.com/a/27166411/1187415.

标签: ios cocoa-touch swift


【解决方案1】:

更简单的方法是在 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 - 1234567890

希望这会有所帮助。

对于 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

【讨论】:

  • 我将在 400 个字符的长段落中使用它,但如果没有更好的方法,它肯定会有所帮助。 (顺便说一句,标签中有swift)
  • 对不起,我没有检查标签。我会尝试在 Swift 中找到相同类型的解决方案。
  • 为 Swift 文本修改教程添加了一个链接
  • 我建议在字符串文件本身中添加指标。例如,此处为粗体。现在代码搜索开始索引和开始索引,这样你就可以得到范围。但是您需要编写位行代码来在整个段落中进行管理。这只是一个提示。
  • 你指的 attrString 在哪里?这应该是 NSMutableAttributedString *attrString 而不是 *String 吗?
【解决方案2】:

感谢 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
}

【讨论】:

  • 这就是我试图传达的内容,将一些指标标签放在字符串文件中。不错的功能,现在对其他人也有用。
  • 可以用 Swift 5 语法更新吗?我尝试了转换但 'NSFontAttributeName' 参数。
【解决方案3】:

//上述答案中方法Esq的Objective 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2011-08-26
    • 2013-02-21
    • 2010-09-17
    相关资源
    最近更新 更多