116

次のような文字列を a に表示したいUILabel:

5 件の結果があります。

数字の 5 は赤で、残りの文字列は黒です。

コードでこれを行うにはどうすればよいですか?

4

20 に答える 20

224

それを行う方法は、次のように使用することですNSAttributedString

NSMutableAttributedString *text = 
 [[NSMutableAttributedString alloc] 
   initWithAttributedString: label.attributedText];

[text addAttribute:NSForegroundColorAttributeName 
             value:[UIColor redColor] 
             range:NSMakeRange(10, 1)];
[label setAttributedText: text];

UILabel それを行うための拡張機能を作成しました。

于 2013-03-01T20:32:23.033 に答える
69

categoryforを作成してこれを行いましたNSMutableAttributedString

-(void)setColorForText:(NSString*) textToFind withColor:(UIColor*) color
{
    NSRange range = [self.mutableString rangeOfString:textToFind options:NSCaseInsensitiveSearch];

    if (range.location != NSNotFound) {
        [self addAttribute:NSForegroundColorAttributeName value:color range:range];
    }
}

のように使う

- (void) setColoredLabel
{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Here is a red blue and green text"];
    [string setColorForText:@"red" withColor:[UIColor redColor]];
    [string setColorForText:@"blue" withColor:[UIColor blueColor]];
    [string setColorForText:@"green" withColor:[UIColor greenColor]];
    mylabel.attributedText = string;
}

スイフト3

extension NSMutableAttributedString{
    func setColorForText(_ textToFind: String, with color: UIColor) {
        let range = self.mutableString.range(of: textToFind, options: .caseInsensitive)
        if range.location != NSNotFound {
            addAttribute(NSForegroundColorAttributeName, value: color, range: range)
        }
    }
}

利用方法

func setColoredLabel() {
    let string = NSMutableAttributedString(string: "Here is a red blue and green text")
    string.setColorForText("red", with: #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1))
    string.setColorForText("blue", with: #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1))
    string.setColorForText("green", with: #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1))
    mylabel.attributedText = string
}

SWIFT 4 @kj13 お知らせありがとうございます

// If no text is send, then the style will be applied to full text
func setColorForText(_ textToFind: String?, with color: UIColor) {

    let range:NSRange?
    if let text = textToFind{
        range = self.mutableString.range(of: text, options: .caseInsensitive)
    }else{
        range = NSMakeRange(0, self.length)
    }
    if range!.location != NSNotFound {
        addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range!)
    }
}

属性を使ってさらに実験を行った結果を以下に示します。これが SOURCECODE です。

これが結果です

スタイル

于 2015-09-30T10:31:54.783 に答える
13
//NSString *myString = @"I have to replace text 'Dr Andrew Murphy, John Smith' ";
NSString *myString = @"Not a member?signin";

//Create mutable string from original one
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:myString];

//Fing range of the string you want to change colour
//If you need to change colour in more that one place just repeat it
NSRange range = [myString rangeOfString:@"signin"];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:(63/255.0) green:(163/255.0) blue:(158/255.0) alpha:1.0] range:range];

//Add it to the label - notice its not text property but it's attributeText
_label.attributedText = attString;
于 2016-03-16T10:23:18.737 に答える
7

iOS 6以降、UIKit は属性付き文字列の描画をサポートしているため、拡張や置換は必要ありません。

からUILabel:

@property(nonatomic, copy) NSAttributedString *attributedText;

を構築するだけですNSAttributedString。基本的に次の 2 つの方法があります。

  1. 同じ属性を持つテキストのチャンクを追加します - 各パーツに対して 1 つのNSAttributedStringインスタンスを作成し、それらを 1 つに追加しますNSMutableAttributedString

  2. プレーン文字列から属性付きテキストを作成し、指定された範囲に属性を追加します。番号の範囲 (または何でも) を見つけて、それに別の色属性を適用します。

于 2013-03-01T20:38:02.687 に答える
3

JTAttributedLabel (by mystcolor) を使用すると、iOS 6 では UILabel の属性付き文字列サポートを使用でき、同時に iOS 5 では JTAutoLabel を介してその JTAttributedLabel クラスを使用できます。

于 2013-03-28T05:59:06.267 に答える
3

NSAttributedString行く方法です。次の質問には、それを行う方法を示す優れた回答があります

于 2011-06-28T06:12:16.903 に答える
2

Swift 3.0 ソリューションがあります

extension UILabel{


    func setSubTextColor(pSubString : String, pColor : UIColor){
        let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: self.text!);
        let range = attributedString.mutableString.range(of: pSubString, options:NSString.CompareOptions.caseInsensitive)
        if range.location != NSNotFound {
            attributedString.addAttribute(NSForegroundColorAttributeName, value: pColor, range: range);
        }
        self.attributedText = attributedString

    }
}

そして call の例があります:

let colorString = " (string in red)"
self.mLabel.text = "classic color" + colorString
self.mLabel.setSubTextColor(pSubString: colorString, pColor: UIColor.red)
于 2016-10-03T19:13:13.423 に答える
2

私の場合、Xcode 10.1 を使用しています。Interface Builder のラベル テキストに、プレーン テキストと属性付きテキストを切り替えるオプションがあります。

ここに画像の説明を入力

これが他の誰かを助けることを願っています..!

于 2019-08-05T11:11:58.450 に答える
0
extension UILabel{

    func setSubTextColor(pSubString : String, pColor : UIColor){


        let attributedString: NSMutableAttributedString = self.attributedText != nil ? NSMutableAttributedString(attributedString: self.attributedText!) : NSMutableAttributedString(string: self.text!);


        let range = attributedString.mutableString.range(of: pSubString, options:NSString.CompareOptions.caseInsensitive)
        if range.location != NSNotFound {
            attributedString.addAttribute(NSForegroundColorAttributeName, value: pColor, range: range);
        }
        self.attributedText = attributedString

    }
}
于 2016-10-04T12:57:40.483 に答える
0

私自身のソリューションは、次のようなメソッドを作成しました:

-(void)setColorForText:(NSString*) textToFind originalText:(NSString *)originalString withColor:(UIColor*)color andLabel:(UILabel *)label{

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:originalString];
NSRange range = [originalString rangeOfString:textToFind];

[attString addAttribute:NSForegroundColorAttributeName value:color range:range];

label.attributedText = attString;

if (range.location != NSNotFound) {
    [attString addAttribute:NSForegroundColorAttributeName value:color range:range];
}
label.attributedText = attString; }

同じテキスト内の 1 つの異なる色だけで機能しましたが、同じ文内でより多くの色に簡単に適応させることができます。

于 2016-11-05T16:58:16.700 に答える
0

SwiftRichString完璧に動作します!+2 つの属性付き文字列を連結するために使用できます

于 2019-07-22T07:20:03.780 に答える