次のような文字列を a に表示したいUILabel
:
5 件の結果があります。
数字の 5 は赤で、残りの文字列は黒です。
コードでこれを行うにはどうすればよいですか?
次のような文字列を a に表示したいUILabel
:
5 件の結果があります。
数字の 5 は赤で、残りの文字列は黒です。
コードでこれを行うにはどうすればよいですか?
それを行う方法は、次のように使用することですNSAttributedString
。
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: label.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(10, 1)];
[label setAttributedText: text];
UILabel
それを行うための拡張機能を作成しました。
category
forを作成してこれを行いました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 です。
これが結果です
//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;
iOS 6以降、UIKit は属性付き文字列の描画をサポートしているため、拡張や置換は必要ありません。
からUILabel
:
@property(nonatomic, copy) NSAttributedString *attributedText;
を構築するだけですNSAttributedString
。基本的に次の 2 つの方法があります。
同じ属性を持つテキストのチャンクを追加します - 各パーツに対して 1 つのNSAttributedString
インスタンスを作成し、それらを 1 つに追加しますNSMutableAttributedString
プレーン文字列から属性付きテキストを作成し、指定された範囲に属性を追加します。番号の範囲 (または何でも) を見つけて、それに別の色属性を適用します。
JTAttributedLabel (by mystcolor) を使用すると、iOS 6 では UILabel の属性付き文字列サポートを使用でき、同時に iOS 5 では JTAutoLabel を介してその JTAttributedLabel クラスを使用できます。
NSAttributedString
行く方法です。次の質問には、それを行う方法を示す優れた回答があります
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)
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
}
}
私自身のソリューションは、次のようなメソッドを作成しました:
-(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 つの異なる色だけで機能しましたが、同じ文内でより多くの色に簡単に適応させることができます。
SwiftRichString
完璧に動作します!+
2 つの属性付き文字列を連結するために使用できます