11

In my application i am using ActiveLabelfram Github.

In that case, my label does not show the text in the middle of the UILabel. If i use a normal UILabel it works fine, but when settings it to a ActiveLabel, it gets like this. enter image description here

(Image is taken in runtime)

I think this is the code to play with the alignment somehow:

/// add line break mode
private func addLineBreak(attrString: NSAttributedString) -> NSMutableAttributedString {
    let mutAttrString = NSMutableAttributedString(attributedString: attrString)

    var range = NSRange(location: 0, length: 0)
    var attributes = mutAttrString.attributesAtIndex(0, effectiveRange: &range)

    let paragraphStyle = attributes[NSParagraphStyleAttributeName] as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
    if let lineSpacing = lineSpacing {
        paragraphStyle.lineSpacing = CGFloat(lineSpacing)
    }

    attributes[NSParagraphStyleAttributeName] = paragraphStyle
    mutAttrString.setAttributes(attributes, range: range)

    return mutAttrString
}

ActiveLabel.swift

ActiveType.swift

Any ideas how i can make it in the middle like this: enter image description here

(Image is taken from Storyboard)

Roduck Nickes
  • 1,021
  • 2
  • 15
  • 41
  • In storyboard add vertical and horizontal constraints to the parent view. Also make sure you are doing that in the Any/Any storyboard view – Halfpint Dec 16 '15 at 01:15
  • @Alex - I am not using Auto Layout in my application. – Roduck Nickes Dec 16 '15 at 01:17
  • 1
    Ah fair enough, May I ask why? It solves a lot of headaches instead of manually creating constraints – Halfpint Dec 16 '15 at 01:18
  • @Alex - Since i started using xCode i've never used Auto Layout. I find it much easier to **not** use it, and i've never had issues with it like this either :) – Roduck Nickes Dec 16 '15 at 01:20
  • 5
    Well good luck to you sir, May I suggest you look into auto layout. I used to define my constraints manually but since I looked into auto layout I've really improved my workflow, each to their own though!! – Halfpint Dec 16 '15 at 01:21
  • @RoduckNickes I know this is not good approach but your string is fixed, so you can add new line characters in the beginning of your string. – Rohit Khandelwal Dec 22 '15 at 05:20

3 Answers3

4

In ActiveLabel.swift replace the drawTextInRect method with

public override func drawTextInRect(rect: CGRect) {
        let range = NSRange(location: 0, length: textStorage.length)

        textContainer.size = rect.size

        let usedRect = layoutManager.usedRectForTextContainer(textContainer)

        let glyphOriginY = (rect.height > usedRect.height) ? rect.origin.y + (rect.height - usedRect.height) / 2 : rect.origin.y
        let glyphOrigin = CGPointMake(rect.origin.x, glyphOriginY)

        layoutManager.drawBackgroundForGlyphRange(range, atPoint: glyphOrigin)
        layoutManager.drawGlyphsForGlyphRange(range, atPoint: glyphOrigin)
    }

I have also forked the repo under https://github.com/rishi420/ActiveLabel.swift

If you download the repo, remember to set verticalTextAlignmentCenter to true

Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • Omg you are a god! :) Another problem using ActiveLabel is that minimum font size is not working, the the font size does not change if there are many words in the string. Could you fix that also? I want that the whole text fit in the UILabel(Active Label), and also resize the font to fit in the UILabel. – Roduck Nickes Dec 22 '15 at 15:02
  • EDIT: I tried adding `minimumScaleFactor = 1`and `adjustsFontSizeToFitWidth = true` over the `userInteractionEnabled = true` in the `private func setupLabel()` but that did nothing. – Roduck Nickes Dec 22 '15 at 17:47
  • @RoduckNickes :) about your second question, forked repo updated. Download form the forked repo. use `minFontSize`. There are lot of room for improvement in the repository. Any body feel free to contribute. – Warif Akhand Rishi Dec 23 '15 at 15:07
  • Awesome! A question - What happens if the creator of ActiveLabel updates it, and i run "pod install" again. Will the `verticalTextAlignement`and `minFontSize`get removed? – Roduck Nickes Dec 23 '15 at 21:34
  • I think they will get removed. I've shared this question link to `ActiveLabel` collaborators, made a pull request to their repository. They could merge my commit or they will refactor code to improve for these features. I think people will get these features in the next release anyways. – Warif Akhand Rishi Dec 24 '15 at 05:49
  • Ah, well anyways! Thank you so much for your help! Merry Christmas. – Roduck Nickes Dec 25 '15 at 01:24
0

Have a look at this post:

Programmatically Add CenterX/CenterY Constraints

Well the problem will be when u have dragged a label already from the IB and then you are trying to change its position. The code will break. You will need to programmatically make the label and then set it to the centre.

And very seriously, @Alex is correct. AutoLayout solves a lot of problems.

Community
  • 1
  • 1
Karthik
  • 99
  • 2
  • 14
-1

You can set the textAlignment in the code like this: showLab.textAlignment = NSTextAlignmentCenter;

Or you can also use Storyboard or xib to see what happen in the lab,StoryBoard,as you look i choose the Second of alignment what means middle in the label

SNTD
  • 1