简体   繁体   English

如何调用返回NSMutableAttributedString的方法

[英]how to call a method that returns NSMutableAttributedString

I have create a method which I would like to create a NSMutableAttributedString that is partly bolded. 我创建了一个方法,该方法想创建一个部分加粗的NSMutableAttributedString。 However I am having some trouble calling it to return the data that it should. 但是我在调​​用它返回它应该返回的数据时遇到了一些麻烦。

This is how I have implements the code 这就是我实现代码的方式

//.h
// creates bold portion of the labels in toolbar
- (NSMutableAttributedString *)createBoldString:(NSString *)labelString intRangeA:(int)rangeA intRangeB:(int)rangeB;





//.m

       - (NSMutableAttributedString *)createBoldString:(NSString *)labelString intRangeA:(int)rangeA intRangeB:(int)rangeB {
            // iOS6 and above : Use NSAttributedStrings
            const CGFloat fontSize = 12;
            UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
            UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
            UIColor *foregroundColor = [UIColor whiteColor];

            // Create the attributes
            NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                   boldFont, NSFontAttributeName,
                                   foregroundColor, NSForegroundColorAttributeName, nil];
            NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                      regularFont, NSFontAttributeName, nil];
            const NSRange range = NSMakeRange(rangeA, rangeB); // range of " 2012/10/14 ". Ideally this should not be hardcoded

            // Create the attributed string (text + attributes)
            NSMutableAttributedString *attributedText =
            [[NSMutableAttributedString alloc] initWithString:labelString
                                                   attributes:attrs];
            [attributedText setAttributes:subAttrs range:range];

            // Set it in our UILabel and we are done!
            return attributedText;
        //    [firstToolBarLabel setAttributedText:attributedText];
        }

and this is how I am trying to call it without sucsess 这就是我试图不成功地称呼它的方式

NSAttributedString *firstAttr = [[NSAttributedString alloc] init];
    [firstAttr create.... // this dose not auto complete and I cannot see the method

I dont know why but I cannot use the method I created. 我不知道为什么,但是我不能使用我创建的方法。 Am I doing it right? 我做对了吗? is there a different way to pass the data back or am i missing something 是否有另一种方式将数据传回,或者我错过了什么

any help would be appreciated. 任何帮助,将不胜感激。

You want to call [self create...] , not [firstAttr create... ]. 您要调用[self create...]而不是[firstAttr create... ]。 Your create... method is an instance method of your object (self). 您的create...方法是您的对象(自身)的实例方法。 It returns an attributed string, but it is not a method of the attributed string class. 返回一个属性串,但它不是属性串类方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM