Is there a unicode way to make part of a string bold?
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 do some of this bold? As there are no strange things! or something like that using Unicode characters? Or in some other way?
I use it like this:
textView.text = "\n\n " + NSLocalizedString("rulesText", comment: "")
source to share
Thanks to Martins' previous answer, I edited his solution to fit my case and it works great.
Check out and dismiss his solution: create an attributed string from plain (Android formatted) 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
it's easy to add additional functions 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
}
source to share
It will be easier to use NSMutableAttributedString when used in textView.text. Here's 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];
It will automatically highlight bold letters starting with the 2nd index and then 4 characters. for example: 1234567890 - 12 3456 7890
Hope it helps.
For SWIFT language:
let font:UIFont? = UIFont.boldSystemFontOfSize(12.0)
myMutableString.addAttribute(NSFontAttributeName, value: font!, range: NSRange(location: 2, length: 4));
source to share
// Objective C format of Esq methods in the 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;
}
source to share
working version in Swift 3. Works with both Label and 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
}
(As of 3.1 there are several problems with range conversion because the "range" parameter in "addAttribute" is still NSRange, not Range. This can be solved by specifying a String to an NSString and getting the "boldRange" range in NSRange
For Swift 4, just change this line
theAttributedString.addAttribute(NSAttributedStringKey.font, value: font, range: boldRange)
source to share