Изменить цвет строки с помощью NSAttributedString?
У меня есть ползунок для опроса, в котором отображаются следующие строки, основанные на значении слайдера: "Очень плохо, плохо, хорошо, хорошо, очень хорошо".
Вот код слайдера:
- (IBAction) sliderValueChanged:(UISlider *)sender {
scanLabel.text = [NSString stringWithFormat:@" %.f", [sender value]];
NSArray *texts=[NSArray arrayWithObjects:@"Very Bad", @"Bad", @"Okay", @"Good", @"Very Good", @"Very Good", nil];
NSInteger sliderValue=[sender value]; //make the slider value in given range integer one.
self.scanLabel.text=[texts objectAtIndex:sliderValue];
}
Я хочу, чтобы "Очень плохо" было красным, "Плохо", чтобы быть оранжевым, "Хорошо", чтобы быть желтым, "Хорошим" и "Очень хорошим" - зеленым.
Я не понимаю, как использовать NSAttributedString
, чтобы это сделать.
Ответы
Ответ 1
Нет необходимости использовать NSAttributedString
. Все, что вам нужно, это простая метка с правильным textColor
. Плюс это простое решение будет работать со всеми версиями iOS, а не только с iOS 6.
Но если вы не хотите использовать NSAttributedString
, вы можете сделать что-то вроде этого:
UIColor *color = [UIColor redColor]; // select needed color
NSString *string = ... // the string to colorize
NSDictionary *attrs = @{ NSForegroundColorAttributeName : color };
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:string attributes:attrs];
self.scanLabel.attributedText = attrStr;
Ответ 2
Используйте что-то вроде этого (не компилятор проверен)
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:self.text.text];
NSRange range=[self.myLabel.text rangeOfString:texts[sliderValue]]; //myLabel is the outlet from where you will get the text, it can be same or different
NSArray *[email protected][[UIColor redColor],
[UIColor redColor],
[UIColor yellowColor],
[UIColor greenColor]
];
[string addAttribute:NSForegroundColorAttributeName
value:colors[sliderValue]
range:range];
[self.scanLabel setAttributedText:texts[sliderValue]];
Ответ 3
В Swift 4:
// Custom color
let greenColor = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 1)
// create the attributed colour
let attributedStringColor = [NSAttributedStringKey.foregroundColor : greenColor];
// create the attributed string
let attributedString = NSAttributedString(string: "Hello World!", attributes: attributedStringColor)
// Set the label
label.attributedText = attributedString
В Swift 3:
// Custom color
let greenColor = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 1)
// create the attributed color
let attributedStringColor : NSDictionary = [NSForegroundColorAttributeName : greenColor];
// create the attributed string
let attributedString = NSAttributedString(string: "Hello World!", attributes: attributedStringColor as? [String : AnyObject])
// Set the label
label.attributedText = attributedString
Наслаждаться.
Ответ 4
Для Swift 4:
var attributes = [NSAttributedStringKey: AnyObject]()
attributes[.foregroundColor] = UIColor.red
let attributedString = NSAttributedString(string: "Very Bad", attributes: attributes)
label.attributedText = attributedString
Для Swift 3:
var attributes = [String: AnyObject]()
attributes[NSForegroundColorAttributeName] = UIColor.red
let attributedString = NSAttributedString(string: "Very Bad", attributes: attributes)
label.attributedText = attributedString
Ответ 5
Вы можете создать NSAttributedString
NSDictionary *attributes = @{ NSForegroundColorAttributeName : [UIColor redColor] };
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"My Color String" attributes:attrs];
ИЛИ NSMutableAttributedString
для применения пользовательских атрибутов с диапазонами.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@", methodPrefix, method] attributes: @{ NSFontAttributeName : FONT_MYRIADPRO(48) }];
[attributedString addAttribute:NSFontAttributeName value:FONT_MYRIADPRO_SEMIBOLD(48) range:NSMakeRange(methodPrefix.length, method.length)];
Доступные атрибуты: NSAttributedStringKey
ОБНОВЛЕНИЕ:
Swift 5.1
let message: String = greeting + someMessage
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 2.0
// Note: UIFont(appFontFamily:ofSize:) is extended init.
let regularAttributes: [NSAttributedString.Key : Any] = [.font : UIFont(appFontFamily: .regular, ofSize: 15)!, .paragraphStyle : paragraphStyle]
let boldAttributes = [NSAttributedString.Key.font : UIFont(appFontFamily: .semiBold, ofSize: 15)!]
let mutableString = NSMutableAttributedString(string: message, attributes: regularAttributes)
mutableString.addAttributes(boldAttributes, range: NSMakeRange(0, greeting.count))
Ответ 6
С Swift 4, NSAttributedStringKey
имеет статическое свойство, называемое foregroundColor
. foregroundColor
имеет следующее объявление:
static let foregroundColor: NSAttributedStringKey
Значение этого атрибута - это объект UIColor
. Используйте этот атрибут, чтобы указать цвет текста во время рендеринга. Если вы не укажете этот атрибут, текст будет выделен черным цветом.
Следующий код игровой площадки показывает, как установить цвет текста экземпляра NSAttributedString
с помощью foregroundColor
:
import UIKit
let string = "Some text"
let attributes = [NSAttributedStringKey.foregroundColor : UIColor.red]
let attributedString = NSAttributedString(string: string, attributes: attributes)
В приведенном ниже коде показана возможная реализация UIViewController
, которая полагается на NSAttributedString
, чтобы обновить цвет текста и текста UILabel
от UISlider
:
import UIKit
enum Status: Int {
case veryBad = 0, bad, okay, good, veryGood
var display: (text: String, color: UIColor) {
switch self {
case .veryBad: return ("Very bad", .red)
case .bad: return ("Bad", .orange)
case .okay: return ("Okay", .yellow)
case .good: return ("Good", .green)
case .veryGood: return ("Very good", .blue)
}
}
static let minimumValue = Status.veryBad.rawValue
static let maximumValue = Status.veryGood.rawValue
}
final class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var slider: UISlider!
var currentStatus: Status = Status.veryBad {
didSet {
// currentStatus is our model. Observe its changes to update our display
updateDisplay()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Prepare slider
slider.minimumValue = Float(Status.minimumValue)
slider.maximumValue = Float(Status.maximumValue)
// Set display
updateDisplay()
}
func updateDisplay() {
let attributes = [NSAttributedStringKey.foregroundColor : currentStatus.display.color]
let attributedString = NSAttributedString(string: currentStatus.display.text, attributes: attributes)
label.attributedText = attributedString
slider.value = Float(currentStatus.rawValue)
}
@IBAction func updateCurrentStatus(_ sender: UISlider) {
let value = Int(sender.value.rounded())
guard let status = Status(rawValue: value) else { fatalError("Could not get Status object from value") }
currentStatus = status
}
}
Обратите внимание, однако, что вам действительно не нужно использовать NSAttributedString
для такого примера и просто полагаться на свойства UILabel
text
и textColor
. Поэтому вы можете заменить реализацию updateDisplay()
на следующий код:
func updateDisplay() {
label.text = currentStatus.display.text
label.textColor = currentStatus.display.color
slider.value = Float(currentStatus.rawValue)
}
Ответ 7
Обновление для Swift 4.2
var attributes = [NSAttributedString.Key: AnyObject]()
attributes[.foregroundColor] = .blue
let attributedString = NSAttributedString(string: "Very Bad",
attributes: attributes)
label.attributedText = attributedString