oscarimonbox
1/15/2016 - 7:47 AM

Cambiar color de una parte de la label

Cambiar color de una parte de la label

Or you can do an extension of UILabel.

  extension UILabel {

      func colorString(text: String?, coloredText: String?, color: UIColor? = .red) {

      let attributedString = NSMutableAttributedString(string: text!)
      let range = (text! as NSString).range(of: coloredText!)
      attributedString.setAttributes([NSForegroundColorAttributeName: color!],
                               range: range)
      self.attributedText = attributedString
  }
To use it.

      self.testLabel.colorString(text: "mehmet alper tabak",
                           coloredText: "alper")
1) componentente TEXTVIEW (quitar scroll y editable pero dejar SELECTABLE)
2) DataDetectors poner LINK
3) configurar text (negritas colores etc) así:

        self.lblText.delegate = self;
        
        let totalRange = NSMakeRange(0, "aboutText".localized.characters.count);
        let range = ("aboutText".localized as NSString).range(of: "aboutLinkText".localized)
        
        let attributedString = NSMutableAttributedString(string:"aboutText".localized)
        
        let totalFont = UIFont(name: "AvenirNext-Regular", size: 11);
        let font = UIFont(name: "AvenirNext-Bold", size: 11);
        let titleParagraphStyle = NSMutableParagraphStyle()
        titleParagraphStyle.alignment = .justified
        
        attributedString.addAttribute(NSFontAttributeName, value: totalFont, range: totalRange)
        attributedString.addAttribute(NSFontAttributeName, value: font, range: range)
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.white , range: totalRange)
        attributedString.addAttribute(NSParagraphStyleAttributeName, value: titleParagraphStyle, range: totalRange)
        self.lblText.attributedText = attributedString;
        
        UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.white ]

        self.lblText.sizeToFit();

4)  Permitir abrir el link
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        // here write your code of navigation
        return true
    }
_lblAddress.text = [NSString stringWithFormat:@"%@ · %@",place.address,distance];

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

[text addAttribute:NSForegroundColorAttributeName 
             value:[UIColor redColor] 
             range:NSMakeRange(1, [place.address length])];
[label setAttributedText: text];

[_lblAddress setAttributedText: text];
    _lblTime.text = [NSString stringWithFormat:NSLocalizedString(@"place_timetable", nil), self.place.workingHours];
    UIFont *boldFont =  [UIFont fontWithName:FONT_BOLD size:13.0];
    
    NSMutableAttributedString *text =
    [[NSMutableAttributedString alloc]
     initWithAttributedString: _lblTime.attributedText];
    
    [text addAttribute:NSFontAttributeName
                 value:boldFont
                 range:NSMakeRange(0, [NSLocalizedString(@"place_timetable", nil) length]-2)];
    
    [_lblTime setAttributedText: text];