Click here to Skip to main content
Click here to Skip to main content

AutoLayout and UILabel Don't Get Along

, 12 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This handy tip is an easy workaround for a shortcoming in iOS 6 and 7.


AutoLayout constraints are a huge improvement for page layout, but it doesn't always act the way it should. Take the UILabel for example. When constrained to another control, and the number of lines is set to 0, the text should be forced to wrap due to the UILabel's constraints. Unfortunately what you get looks like this. The constraint on the UILabel is ignored.


Luckily there is a simple workaround, shown here in C# (for Xamarin.iOS).

using MonoTouch.UIKit;

namespace RedCell.UI.Controls
    /// <summary>
    /// A workaround to allow wrapping with Auto Layout.
    /// </summary>
    internal class CustomUILabel : UILabel
        #region Overrides of UIView
        /// <summary>
        /// Lays out subviews.
        /// </summary>
        public override void LayoutSubviews ()
            this.PreferredMaxLayoutWidth = this.Superview.Bounds.Size.Width;

Subclassing UIView and overriding LayoutSubviews, we can set PreferredMaxLayoutWidth to the width of the control's parent, resulting the expected result:

Simply use the new class in place of UILabel.


  • December 9, 2013: Initial publication.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Yvan Rodrigues
President Red Cell Innovation Inc.
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the manufacturing sector. He runs Red Cell Innovation Inc./L'innovation de Globules Rouges, a consulting company focusing on efficiency and automation of manufacturing and business processes for small businesses, healthcare, and the public sector. He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).
Yvan draws on experience at Mabel's Labels Inc. as Manager of Systems and Development, and the University of Waterloo as Information Systems Manager.
Yvan supports open-source software. He is a committer for SharpKit (C# to Javascript cross-compiler) and WebIssues (Issue/Ticket Management System), TinyMCE (JavaScript editor), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, and Xamarin.
Yvan's consumer-focused apps can be found in the Windows Store, Apple App Store, and Google Play marketplace.
Follow on   Google+   LinkedIn

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 12 Dec 2013
Article Copyright 2013 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid