2015-01-25 2 views
4

UITextViewtextview.selectable = true이 있고 UIButton을 사용하여 선택한 TextColor를 변경하려면 어떻게해야합니까?UITextView 선택한 텍스트의 색

+0

수정 사항을 찾았습니까? 그렇다면 정답을 게시하십시오. – Cesare

답변

6

문자열의 선택한 범위를 변경하려면 attributedText 속성을 변경해야합니다.

@IBAction func didTapButton(sender: UIButton) { 
    let range = textView.selectedRange 
    let string = NSMutableAttributedString(attributedString: textView.attributedText) 
    let attributes = [NSForegroundColorAttributeName: UIColor.redColor()] 
    string.addAttributes(attributes, range: textView.selectedRange) 
    textView.attributedText = string 
    textView.selectedRange = range 
} 

전체 문자열을 변경하려면 CeceXX에서 제안한 기술을 사용할 수 있습니다.

@IBAction func didTapButton(sender: UIButton) { 
    textView.textColor = UIColor.redColor() 
} 
관련 문제