2016-08-24 2 views
2

Ich habe diesen Code in meinen Händen:Wie mache ich einen Teil von NSMutableString fett?

if let text = trimText?.mutableCopy() as? NSMutableString { 
    text.insertString("\(prefix) ", atIndex: 0) 
    textStorage.replaceCharactersInRange(range, withString: text as String) 
} 

Wenn ich versuche, meine ändern text wie:

text = attributedTextFunc(text) 

wo

func attributedTextFunc(str: NSString) -> NSAttributedString { 

    var attributedString = NSMutableAttributedString(string: str as String, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(15.0)]) 

    let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(15.0)] 

    attributedString.addAttributes(boldFontAttribute, range: str.rangeOfString("More")) 

    return attributedString 
} 

und ich bekomme diese Fehlermeldung:

Cannot assign value of type 'NSAttributedString' to type 'NSMutableString' 

Wie kann ich es fett machen?

Antwort

2

Sie können NSAttributedString nicht als Text verwenden. Es gibt zwei verschiedene Arten.

String ist nicht von NSAttributedString abgeleitet.

sollten Sie setzen:

attributedText = attributedTextFunc(text) 

Dann, wenn Sie es auf UILabel präsentieren wollen

label.attributedText = attributedText 

UPDATE

Struct String weiß nichts über UIKit und Bold Stile .

NSAttributedString kennt UIKit und enthält alle Textstile Sie

wollen

UPDATE 2

In Ihrem Fall

ReadMoreTextView.attributedTrimText = attributedText 
+0

Ich verwende diese Bibliothek https://github.com/ilyapuchka/ReadMoreTextView, und ich möchte die zugeschnittene Textfarbe ändern und mach es fett. Aber ich kann nicht =/ – Doe

+0

Ja, Sie sollten es in dieser Bibliothek tun. – Konstantin

+0

@Doe Sie möchten einen fettgedruckten 'Read More' Text erstellen? –

0

Es liegt daran, dass Ihre text Art von NSMutableString und Ihre Funktion ist attributedTextFunc ist der Typ von NSString.

Das ist das Problem, also ändern Sie es einfach von NSString zu NSMutableString.

+0

NSMutableString ist Unterklasse von NSString, Problem ist in NSAttributedString. NSAtributedString ist keine Unterklasse von NSString – Konstantin

2

Sie können nicht neu zuweisen text weil:

  • text konstant (let)
  • textNSMutableString, aber attributedTextFunc Rückkehr NSAttributedString

Sie haben als Folge der attributedTextFunc in Variablen zu speichern NSAttributeString und attributeText von UILabel ins tead von text

if let text = trimText?.mutableCopy() as? NSMutableString { 
    // ... 
    let attributeText = attributedTextFunc(text) 
    someLabel.attributeText = attributeText 
} 
0

Verwenden Sie diesen Code und geben Sie Ihre normalen String und Bold String (die fett zu sein, ist nicht erforderlich).

func attributeStrings(first: String, second : String) -> NSMutableAttributedString{ 
     let myNormalAttributedTitle = NSAttributedString(string: first, 
                 attributes: [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]) 
     let myAttributedTitle = NSAttributedString(string: second, 
                attributes: [NSForegroundColorAttributeName : UIColor.blackColor()]) 
     let result = NSMutableAttributedString() 
     result.appendAttributedString(myNormalAttributedTitle) 
     result.appendAttributedString(myAttributedTitle) 
     return result 
    } 

und weisen Sie den Rückgabewert dieser Funktion zu

someLabel.attributeText = attributeStrings("My Name is", second : "Himanshu") 
Verwandte Themen