Any way to bold part of a NSString?

2019-01-02 23:21发布

Is there any way to bold only part of a string? For example:

Approximate Distance: 120m away

Thanks!

10条回答
我只想做你的唯一
2楼-- · 2019-01-02 23:26

Swift

Also includes getting the range of the string you want to embolden dynamically

let nameString = "Magoo"
let string = "Hello my name is \(nameString)"

let attributes = [NSFontAttributeName:UIFont.systemFontOfSize(14.0),NSForegroundColorAttributeName: UIColor.black]
let boldAttribute = [NSFontAttributeName:UIFont.boldSystemFontOfSize(14.0)]

let attributedString = NSMutableAttributedString(string: string, attributes: attributes)

let nsString = NSString(string: string)
let range = nsString.rangeOfString(nameString)

if range.length > 0 { attributedString.setAttributes(boldAttribute, range: range) }

someLabel.attributedText = attributedString
查看更多
▲ chillily
3楼-- · 2019-01-02 23:27

The code above gave me crash when I created UILabel with this attributedString.

I used this code and it worked:

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange boldedRange = NSMakeRange(0, 1);
UIFont *fontText = [UIFont systemFontOfSize:12]; //[UIFont fontWithName:@"Lato-Bold" size:12];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];
[attrString setAttributes:dictBoldText range:boldedRange];
查看更多
男人必须洒脱
4楼-- · 2019-01-02 23:27

In Xamarin ios you can bold part of a NSString this way:

public static NSMutableAttributedString BoldRangeOfString (string str, float fontSize, int startRange, int lengthRange)
    {
        var firstAttributes = new UIStringAttributes {
            Font = UIFont.BoldSystemFontOfSize(fontSize)
        };

        NSMutableAttributedString boldString = new NSMutableAttributedString (str);
        boldString.SetAttributes (firstAttributes.Dictionary, new NSRange (startRange, lengthRange));
        return boldString;
    }    

and call this method:

myLabel = new UILabel (); 
...
myLabel.AttributedText = BoldRangeOfString("my text", fontSize, startRange, lengthRange);    
查看更多
仙女界的扛把子
5楼-- · 2019-01-02 23:28

To bold a string without hardcoding its font, you can use the StrokeWidth attribute with a negative value:

let s = NSMutableAttributedString(string: "Approximate Distance: 120m away")
s.addAttribute(NSStrokeWidthAttributeName, value: NSNumber(value: -3.0), range: NSRange(22..<26))
查看更多
时光不老,我们不散
6楼-- · 2019-01-02 23:29

If you do not want to bother with fonts (as not every variation of font contains "Bold"), here is another way to do this. Please be aware, this is currently only available on OS X...:

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:"Approximate Distance: 120m away"];
[attrString beginEditing];
[attrString applyFontTraits:NSBoldFontMask
                      range:NSMakeRange(22, 4)];
[attrString endEditing];
查看更多
Animai°情兽
7楼-- · 2019-01-02 23:37

An NSString is just a data container. It doesn't contain any details about presentation concerns.

It sounds like what you probably want to do is bold part of the UILabel that is being used to display your string. Which I don't think you can do. But you could always break the UI down into three labels, one for "Approximate Distance:", one for "120 m", and one for "away". Place them in-line with each other and you should get the desired effect.

Another option might be to use a UIWebView and a little bit of markup to display your string with embedded formatting information, as discussed here:

http://iphoneincubator.com/blog/windows-views/display-rich-text-using-a-uiwebview

查看更多
登录 后发表回答