Programing

NSMutableAttributedString에서 링크 색상 변경

lottogame 2020. 11. 26. 07:46
반응형

NSMutableAttributedString에서 링크 색상 변경


다음 코드가 있지만 링크는 항상 파란색입니다. 색깔을 어떻게 바꾸나요?

[_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)];
[_string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(12.0)] range:NSMakeRange(position, length)];
[_string addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(position, length)];

_string은 NSMutableAttributedString이고 위치와 길이가 잘 작동합니다.


빠른

Swift 4.2 업데이트

linkTextAttributes함께 사용UITextView

textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]

그리고 맥락에서 :

let attributedString = NSMutableAttributedString(string: "The site is www.google.com.")
let linkRange = (attributedString.string as NSString).range(of: "www.google.com")
attributedString.addAttribute(NSAttributedString.Key.link, value: "https://www.google.com", range: linkRange)
let linkAttributes: [NSAttributedString.Key : Any] = [
    NSAttributedString.Key.foregroundColor: UIColor.green,
    NSAttributedString.Key.underlineColor: UIColor.lightGray,
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]

// textView is a UITextView
textView.linkTextAttributes = linkAttributes
textView.attributedText = attributedString

목표 -C

linkTextAttributes함께 사용UITextView

textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]};

출처 : 이 답변

그리고이 게시물에서 :

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
                         value:@"username://marcelofabri_"
                         range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];


NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
                                 NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};

// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;

링크 색상은 레이블 / textView의 색조 색상입니다. 따라서 뷰의 색조 색상을 변경하여 변경할 수 있습니다. 그러나 동일한보기 내에서 다른 링크 색상을 원하는 경우에는 작동하지 않습니다.


빠른

 let str = "By using this app you agree to our Terms and Conditions and Privacy Policy"
 let attributedString = NSMutableAttributedString(string: str)
 var foundRange = attributedString.mutableString.rangeOfString("Terms and Conditions")

 attributedString.addAttribute(NSLinkAttributeName, value: termsAndConditionsURL, range: foundRange)
 foundRange = attributedString.mutableString.rangeOfString("Privacy Policy")
 attributedString.addAttribute(NSLinkAttributeName, value: privacyURL, range: foundRange)
 policyAndTermsTextView.attributedText = attributedString
 policyAndTermsTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.blueColor()]

 NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"here" attributes:@{ @"myCustomTag" : @(YES), NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) }];

목표 -C

그러면 클릭 가능한 밑줄이 그어진 흰색 텍스트가됩니다. 코드에 필요한 속성을 선택하고 사용하십시오.

클릭 가능한 링크가있는 문자열을 포함하려면 다음을 수행하십시오.

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Click " attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15]}];
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"here" attributes:@{ @"myCustomTag" : @(YES), NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) }];
[string appendAttributedString:attributedString];

As a result you will get string 'Click here' and 'here' will be a link. You can set different styles to each string.


For swift3.0

  override func viewDidLoad() {
     super.viewDidLoad()

  let linkAttributes = [
        NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
        ] as [String : Any]
  let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")

  attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))

  attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))

  textview.delegate = self
  textview.attributedText = attributedString
  textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
  textview.textColor = UIColor.white
  }


  func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    return true
   }

참고URL : https://stackoverflow.com/questions/28361072/change-the-color-of-a-link-in-an-nsmutableattributedstring

반응형