2014-04-14 4 views
0

selectedElement에있는 텍스트에 밑줄을 써야하는 버튼을 만들었습니다.텍스트에 밑줄을 긋는 단추를 만드는 방법은 무엇입니까?

나는 지금까지이 코드가 있지만 텍스트를 강조 표시 한 다음 밑줄을 누르면 작동합니다. 텍스트를 강조 표시하지 않고 밑줄을 그어 줘야합니다. 밑줄 버튼을 다시 누르면 언더 라인을 실행 취소하는 방법에 대한 도움말도 환영합니다. 시간 내 주셔서 대단히 감사합니다! 나는) 초보자를 해요 :

-(void)underlineTextButton{ 
    UITextView selectedText = (UITextView) selectedElement; 
    NSRange range = selectedText.selectedRange; 
    NSTextStorage *textStorage = selectedText.textStorage; 
    [textStorage addAttribute: NSUnderlineStyleAttributeName 
    value:[NSNumber numberWithInt:NSUnderlineStyleSingle] 
    range:range]; 
} 
+2

가 [NSAttributedString은] 확인하시기 바랍니다 (https://developer.apple.com/library/mac/documentation/cocoa/reference/ 기초/클래스/NSAttributedString_Class/Reference/Reference.html). – cojoj

+0

아직도 운이 없음) : – Camerz007

답변

0

사용할 수 :

-(void) viewDidLoad 
{ 
    //If it gonna be the static text 
    _textView.attributedText = [[NSAttributedText alloc] initWithString:@"Your <u>underlined</u> text goes here.."]; 

    // If need to by text using regex 
    NSRange r; 
    NSString *s = _textView.text; 
    while ((r = [s rangeOfString:@"patternText" options:NSRegularExpressionSearch]).location != NSNotFound) 
    s = [s stringByReplacingCharactersInRange:r withString:[NSString stringWithFormat:@"<u>%@</u>", patternText]]; 
    _textView.attributtedText = [[NSAttributedText alloc] initWithString:s]; 

} 
관련 문제