6

in Localizable.strings

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

Can I make part of this bold? Like No weird stuff! or something in this sense using unicode characters? Or some other way?

I use it like this:

textView.text = "\n\n " + NSLocalizedString("rulesText", comment: "")
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
  • Where are you using "rulesText" in which widget, textview, webview ? – Mrunal Dec 10 '14 at 09:28
  • textview.edited the question – Esqarrouth Dec 10 '14 at 09:28
  • 5
    Unicode is code table for a string, as in the characters not the way a string is presented to the user. In Unicode there are more characters to allow for none latin letters for which are not available in ASCII. You should understand the Unicode has therefor nothing to do with the way it is present to the user. – rckoenes Dec 10 '14 at 09:34
  • u can use Attributted string for it – the1pawan Dec 10 '14 at 09:35
  • You could use HTML tags (such as ``) in the Localizable.strings file and then create an attributed string from HTML, see http://stackoverflow.com/questions/19921972/parsing-html-into-nsattributedtext-how-to-set-font for an example. – Martin R Dec 10 '14 at 09:43
  • 1
    ... translated to Swift here: http://stackoverflow.com/a/27166411/1187415. – Martin R Dec 10 '14 at 09:49
  • thanks @MartinR editing your answer worked. i posted my current solution. any extra tips will be appreciated. – Esqarrouth Dec 10 '14 at 13:03

3 Answers3

7

Easier way would be using NSMutableAttributedString, while using in textView.text. Here is an example:

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];

Using this it will automatically bold characters which starts from 2nd index and then 4 characters. for example: 1234567890 - 1234567890

Hope this helps.

For SWIFT language:

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

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • i am going to use this in a long paragraph of 400 characters, but it will certainly help if there is no better way. (btw there is swift in the tags) – Esqarrouth Dec 10 '14 at 09:40
  • Oh sorry, I have not check tags. I will try to find the same type of solution in Swift. – Mrunal Dec 10 '14 at 09:43
  • Added one link for Swift Text Modification Tutorial – Mrunal Dec 10 '14 at 09:44
  • the problem with this method is, in a different language different parts are going to be bold. which will make the use of nsmakeranges really hard – Esqarrouth Dec 10 '14 at 09:48
  • 3
    I would suggest to add indicator in strings file itself. For example, Bold Text Here. Now code to search start index and start index , so you can get the Range. But you require to write bit line of codes to manage this in entire paragraph. This is just a hint. – Mrunal Dec 10 '14 at 09:51
  • @Esq : If this answer had been useful to you, then can you please upvote and accept the same. – Mrunal Dec 10 '14 at 12:09
  • 1
    where is the attrString you are referring to ? Should this be NSMutableAttributedString *attrString instead and not *String ? – Naishta May 23 '17 at 05:11
5

Thanks to Martins previous answer I edited his solution to match my case and it works perfectly.
Check out and upvote his solution: create an attributed string out of plain (Android formated) text in swift for iOS

So this basically changes:

<a>hey</a> to size 14 bold  
<b>hey</b> to size 12 bold  
<u>hey</u> to underlined  

its easy to add more features to it.

//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
}
Community
  • 1
  • 1
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
  • 1
    This is what I was trying to convey yday, to put some indicator tags in string files. Nice function, now it will be useful to others as well. – Mrunal Dec 11 '14 at 08:58
  • Can update with Swift 5 syntax? I tried with conversion but 'NSFontAttributeName' param. – Jayprakash Dubey Sep 28 '21 at 02:18
0

//Objective C format of the methods Esq in above answer to make attributed strings for 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;
}
Community
  • 1
  • 1