How can I add padding to the intrinsic content size of UILabel?

aslisabanci

I'm using autolayout on iOS7 and I have a problem like this:

I'm putting a UILabel onto a UIView and I'm arranging my autolayout constraints so that the label's centerX = parent view's centerX. I'm not giving any width constraint to the label. When I set the label's text on runtime, the label is drawn just wide enough for the text to fit, there are no margins/paddings on the left and right sides. What I want is to have some padding on the left and right sides, so that the text doesn't begin just where the label begins. The hack to achieve this could be setting the text as @" text " but of course that's not the way to go :)

How can I achieve what I want?

Vincent

You can extend UILabel and override the intrinsicContentSize by yourself. Please make sure you have set the textAlignment = NSTextAlignmentCenter as well.

-(CGSize)intrinsicContentSize{
    CGSize contentSize = [super intrinsicContentSize];
    return CGSizeMake(contentSize.width + 50, contentSize.height);
}

Swift 5.0

open override var intrinsicContentSize: CGSize {
    let size = super.intrinsicContentSize
    return CGSize(width: size.width + 16, height: size.height)
}

This probably only works when you only have just one line of text to display.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can I zero just the padding bytes of a class?

来自分类Dev

How can I reduce the size of my git repo?

来自分类Dev

How can I show the size of all files in my ~/.cache directory?

来自分类Dev

How can I duplicate files and folders without content?

来自分类Dev

How can I add values through XML in OpenERP7?

来自分类Dev

How can I add a space in between two outputs?

来自分类Dev

How can I add a custom Parameter to a box in maxscript?

来自分类Dev

How can I add a context menu to a browser action with a Chrome extension?

来自分类Dev

How can I change the size of UI Collection View cells depending on whether it is iPhone or iPad

来自分类Dev

How can I run Neo4j with larger heap size, specify -server and correct GC strategy

来自分类Dev

How can I get a custom Content-Disposition line using Apache httpclient?

来自分类Dev

How to make the text content in a div increase and decrease in font size repeatedly?

来自分类Dev

Twitter Bootstrap 3 - how can I switch from a horizontal button group to vertical button group based on screen size?

来自分类Dev

odoo 14: how i can add additional fields in SignUp like address etc

来自分类Dev

How can I add a customized buttons to the AlloyUI Scheduler's event popup?

来自分类Dev

How can I add more button on my Android application which already includes a scrollview?

来自分类Dev

How can I add a computed field using an arbitrary function with Mongo aggregate framework?

来自分类Dev

How can I add a folder to "allowed_resources" in resource_config.json?

来自分类Dev

How can I add/update a submodule, using an existing repository to save bandwidth?

来自分类Dev

How can I add imported jar files to web-inf/lib in eclipse for my build?

来自分类Dev

How I can add values to WCF session and use it in Client(WinForm) and in WCF context?

来自分类Dev

How do I define the size of a CollectionView on rotate

来自分类Dev

Can I add a wildcard dependency to a deb package?

来自分类Dev

How to add a click handler to content page in xamarin forms

来自分类Dev

How to add a <hr> to the top of a <td> while keeping the content centered?

来自分类Dev

Add padding to all childs except last

来自分类Dev

如何在子主题的functions.php中覆盖父主题的add_image_size和$ content_width

来自分类Dev

Get content view size in onCreate

来自分类Dev

How can I cancel an ngEvent?

Related 相关文章

  1. 1

    How can I zero just the padding bytes of a class?

  2. 2

    How can I reduce the size of my git repo?

  3. 3

    How can I show the size of all files in my ~/.cache directory?

  4. 4

    How can I duplicate files and folders without content?

  5. 5

    How can I add values through XML in OpenERP7?

  6. 6

    How can I add a space in between two outputs?

  7. 7

    How can I add a custom Parameter to a box in maxscript?

  8. 8

    How can I add a context menu to a browser action with a Chrome extension?

  9. 9

    How can I change the size of UI Collection View cells depending on whether it is iPhone or iPad

  10. 10

    How can I run Neo4j with larger heap size, specify -server and correct GC strategy

  11. 11

    How can I get a custom Content-Disposition line using Apache httpclient?

  12. 12

    How to make the text content in a div increase and decrease in font size repeatedly?

  13. 13

    Twitter Bootstrap 3 - how can I switch from a horizontal button group to vertical button group based on screen size?

  14. 14

    odoo 14: how i can add additional fields in SignUp like address etc

  15. 15

    How can I add a customized buttons to the AlloyUI Scheduler's event popup?

  16. 16

    How can I add more button on my Android application which already includes a scrollview?

  17. 17

    How can I add a computed field using an arbitrary function with Mongo aggregate framework?

  18. 18

    How can I add a folder to "allowed_resources" in resource_config.json?

  19. 19

    How can I add/update a submodule, using an existing repository to save bandwidth?

  20. 20

    How can I add imported jar files to web-inf/lib in eclipse for my build?

  21. 21

    How I can add values to WCF session and use it in Client(WinForm) and in WCF context?

  22. 22

    How do I define the size of a CollectionView on rotate

  23. 23

    Can I add a wildcard dependency to a deb package?

  24. 24

    How to add a click handler to content page in xamarin forms

  25. 25

    How to add a <hr> to the top of a <td> while keeping the content centered?

  26. 26

    Add padding to all childs except last

  27. 27

    如何在子主题的functions.php中覆盖父主题的add_image_size和$ content_width

  28. 28

    Get content view size in onCreate

  29. 29

    How can I cancel an ngEvent?

热门标签

归档