Why does my playground crash on NSRange()










0















I'm trying to learn about NSMutableAttributedString and NSAttributedString so I've created a simple playground to try some things. However, I have a couple of issues I can't figure out even after looking at a lot of examples on SO (like here and NSRange from Swift Range? elsewhere.



The problem is with the length property. If I specify the length as attribLabelText.length it causes an uncaught exception. If I specify attribLabelText.length - 1 there's no error, but only the letters 'Repor' have the attributes I'm setting:



enter image description here



import UIKit
import PlaygroundSupport

class MyViewController : UIViewController
override func loadView()
let view = UIView()
view.backgroundColor = .white

let label = getLabel1(labelText: "Report")
view.addSubview(label)
self.view = view



func getLabel1(labelText: String) -> UILabel
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.textColor = .black

let attribLabelText: NSMutableAttributedString = NSMutableAttributedString(string: labelText)
let attributes = [
NSAttributedString.Key.foregroundColor : UIColor.gray.cgColor,
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14)
] as [NSAttributedString.Key : Any]

attribLabelText.addAttributes(attributes, range: NSRange(location: 0, length: attribLabelText.length)) <-- this causes an uncaught exception of type NSException

label.attributedText = attribLabelText

return label

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()


I have a feeling this is going to be something obvious but I'm out of ideas to try. Can anybody point out what I'm doing wrong?










share|improve this question

















  • 1





    You should be passing a UIColor for the foreground color, not a CGColor

    – dan
    Nov 13 '18 at 16:27











  • @dan - I was about to tell you, "Thanks, but that has nothing to do with my question" and then I changed UIColor.gray.cgColor to UIColor.gray and it fixed the NSRange problem. So, thanks, but could you a) make it an answer so I can accept it and b) explain why this worked?

    – Jim
    Nov 13 '18 at 16:33















0















I'm trying to learn about NSMutableAttributedString and NSAttributedString so I've created a simple playground to try some things. However, I have a couple of issues I can't figure out even after looking at a lot of examples on SO (like here and NSRange from Swift Range? elsewhere.



The problem is with the length property. If I specify the length as attribLabelText.length it causes an uncaught exception. If I specify attribLabelText.length - 1 there's no error, but only the letters 'Repor' have the attributes I'm setting:



enter image description here



import UIKit
import PlaygroundSupport

class MyViewController : UIViewController
override func loadView()
let view = UIView()
view.backgroundColor = .white

let label = getLabel1(labelText: "Report")
view.addSubview(label)
self.view = view



func getLabel1(labelText: String) -> UILabel
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.textColor = .black

let attribLabelText: NSMutableAttributedString = NSMutableAttributedString(string: labelText)
let attributes = [
NSAttributedString.Key.foregroundColor : UIColor.gray.cgColor,
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14)
] as [NSAttributedString.Key : Any]

attribLabelText.addAttributes(attributes, range: NSRange(location: 0, length: attribLabelText.length)) <-- this causes an uncaught exception of type NSException

label.attributedText = attribLabelText

return label

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()


I have a feeling this is going to be something obvious but I'm out of ideas to try. Can anybody point out what I'm doing wrong?










share|improve this question

















  • 1





    You should be passing a UIColor for the foreground color, not a CGColor

    – dan
    Nov 13 '18 at 16:27











  • @dan - I was about to tell you, "Thanks, but that has nothing to do with my question" and then I changed UIColor.gray.cgColor to UIColor.gray and it fixed the NSRange problem. So, thanks, but could you a) make it an answer so I can accept it and b) explain why this worked?

    – Jim
    Nov 13 '18 at 16:33













0












0








0








I'm trying to learn about NSMutableAttributedString and NSAttributedString so I've created a simple playground to try some things. However, I have a couple of issues I can't figure out even after looking at a lot of examples on SO (like here and NSRange from Swift Range? elsewhere.



The problem is with the length property. If I specify the length as attribLabelText.length it causes an uncaught exception. If I specify attribLabelText.length - 1 there's no error, but only the letters 'Repor' have the attributes I'm setting:



enter image description here



import UIKit
import PlaygroundSupport

class MyViewController : UIViewController
override func loadView()
let view = UIView()
view.backgroundColor = .white

let label = getLabel1(labelText: "Report")
view.addSubview(label)
self.view = view



func getLabel1(labelText: String) -> UILabel
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.textColor = .black

let attribLabelText: NSMutableAttributedString = NSMutableAttributedString(string: labelText)
let attributes = [
NSAttributedString.Key.foregroundColor : UIColor.gray.cgColor,
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14)
] as [NSAttributedString.Key : Any]

attribLabelText.addAttributes(attributes, range: NSRange(location: 0, length: attribLabelText.length)) <-- this causes an uncaught exception of type NSException

label.attributedText = attribLabelText

return label

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()


I have a feeling this is going to be something obvious but I'm out of ideas to try. Can anybody point out what I'm doing wrong?










share|improve this question














I'm trying to learn about NSMutableAttributedString and NSAttributedString so I've created a simple playground to try some things. However, I have a couple of issues I can't figure out even after looking at a lot of examples on SO (like here and NSRange from Swift Range? elsewhere.



The problem is with the length property. If I specify the length as attribLabelText.length it causes an uncaught exception. If I specify attribLabelText.length - 1 there's no error, but only the letters 'Repor' have the attributes I'm setting:



enter image description here



import UIKit
import PlaygroundSupport

class MyViewController : UIViewController
override func loadView()
let view = UIView()
view.backgroundColor = .white

let label = getLabel1(labelText: "Report")
view.addSubview(label)
self.view = view



func getLabel1(labelText: String) -> UILabel
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.textColor = .black

let attribLabelText: NSMutableAttributedString = NSMutableAttributedString(string: labelText)
let attributes = [
NSAttributedString.Key.foregroundColor : UIColor.gray.cgColor,
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14)
] as [NSAttributedString.Key : Any]

attribLabelText.addAttributes(attributes, range: NSRange(location: 0, length: attribLabelText.length)) <-- this causes an uncaught exception of type NSException

label.attributedText = attribLabelText

return label

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()


I have a feeling this is going to be something obvious but I'm out of ideas to try. Can anybody point out what I'm doing wrong?







swift nsattributedstring nsrange nsmutableattributedstring






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:13









JimJim

610818




610818







  • 1





    You should be passing a UIColor for the foreground color, not a CGColor

    – dan
    Nov 13 '18 at 16:27











  • @dan - I was about to tell you, "Thanks, but that has nothing to do with my question" and then I changed UIColor.gray.cgColor to UIColor.gray and it fixed the NSRange problem. So, thanks, but could you a) make it an answer so I can accept it and b) explain why this worked?

    – Jim
    Nov 13 '18 at 16:33












  • 1





    You should be passing a UIColor for the foreground color, not a CGColor

    – dan
    Nov 13 '18 at 16:27











  • @dan - I was about to tell you, "Thanks, but that has nothing to do with my question" and then I changed UIColor.gray.cgColor to UIColor.gray and it fixed the NSRange problem. So, thanks, but could you a) make it an answer so I can accept it and b) explain why this worked?

    – Jim
    Nov 13 '18 at 16:33







1




1





You should be passing a UIColor for the foreground color, not a CGColor

– dan
Nov 13 '18 at 16:27





You should be passing a UIColor for the foreground color, not a CGColor

– dan
Nov 13 '18 at 16:27













@dan - I was about to tell you, "Thanks, but that has nothing to do with my question" and then I changed UIColor.gray.cgColor to UIColor.gray and it fixed the NSRange problem. So, thanks, but could you a) make it an answer so I can accept it and b) explain why this worked?

– Jim
Nov 13 '18 at 16:33





@dan - I was about to tell you, "Thanks, but that has nothing to do with my question" and then I changed UIColor.gray.cgColor to UIColor.gray and it fixed the NSRange problem. So, thanks, but could you a) make it an answer so I can accept it and b) explain why this worked?

– Jim
Nov 13 '18 at 16:33












1 Answer
1






active

oldest

votes


















1














You should be passing a UIColor for the foreground color instead of a CGColor.



UILabel seems to use the foreground color attribute slightly differently when drawing the string based on whether the attribute covers the entire range of the string or just a subrange.



The version it uses when the attribute covers the entire string only works with UIColor but the version it uses when the attribute only covers a substring seems to also work with CGColor (though this behavior isn't documented so it shouldn't be relied on) which explains why adding the -1 to the range avoids the exception.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285118%2fwhy-does-my-playground-crash-on-nsrange%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You should be passing a UIColor for the foreground color instead of a CGColor.



    UILabel seems to use the foreground color attribute slightly differently when drawing the string based on whether the attribute covers the entire range of the string or just a subrange.



    The version it uses when the attribute covers the entire string only works with UIColor but the version it uses when the attribute only covers a substring seems to also work with CGColor (though this behavior isn't documented so it shouldn't be relied on) which explains why adding the -1 to the range avoids the exception.






    share|improve this answer



























      1














      You should be passing a UIColor for the foreground color instead of a CGColor.



      UILabel seems to use the foreground color attribute slightly differently when drawing the string based on whether the attribute covers the entire range of the string or just a subrange.



      The version it uses when the attribute covers the entire string only works with UIColor but the version it uses when the attribute only covers a substring seems to also work with CGColor (though this behavior isn't documented so it shouldn't be relied on) which explains why adding the -1 to the range avoids the exception.






      share|improve this answer

























        1












        1








        1







        You should be passing a UIColor for the foreground color instead of a CGColor.



        UILabel seems to use the foreground color attribute slightly differently when drawing the string based on whether the attribute covers the entire range of the string or just a subrange.



        The version it uses when the attribute covers the entire string only works with UIColor but the version it uses when the attribute only covers a substring seems to also work with CGColor (though this behavior isn't documented so it shouldn't be relied on) which explains why adding the -1 to the range avoids the exception.






        share|improve this answer













        You should be passing a UIColor for the foreground color instead of a CGColor.



        UILabel seems to use the foreground color attribute slightly differently when drawing the string based on whether the attribute covers the entire range of the string or just a subrange.



        The version it uses when the attribute covers the entire string only works with UIColor but the version it uses when the attribute only covers a substring seems to also work with CGColor (though this behavior isn't documented so it shouldn't be relied on) which explains why adding the -1 to the range avoids the exception.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 16:40









        dandan

        7,04912734




        7,04912734





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285118%2fwhy-does-my-playground-crash-on-nsrange%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo