how to wrap the text in uiSegmentedControl in swift - ios

I would like to wrap the text of my uiSegmentedControl to have more then one line in it.
Actually it looks like this. You see the middle option reads "search in title an...", which should be wrapped :
My Code is:
var sSearchinTitle=getStringResourceByName("searchtitle_" + AppProperties.sharedInstance.sAppLanguage);
var sSearchinPreface=getStringResourceByName("searchpreface_" + AppProperties.sharedInstance.sAppLanguage);
var sSearchOverall=getStringResourceByName("searchoverall_" + AppProperties.sharedInstance.sAppLanguage);
arraySearchTypes = [sSearchinTitle, sSearchinPreface, sSearchOverall];
segmentcontrol_SearchType = UISegmentedControl(items: arraySearchTypes);
let font = UIFont.systemFontOfSize(fFontsize_SearchtypeValues)
segmentcontrol_SearchType.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
segmentcontrol_SearchType.frame.origin = CGPoint(x: spaceYElements, y: drawy);
segmentcontrol_SearchType.selectedSegmentIndex = saved_TextSizeTextViewer;
segmentcontrol_SearchType.sizeToFit();
self.view.addSubview(segmentcontrol_SearchType);
var segmentcontrolheigth=segmentcontrol_SearchType.frame.height;
var segmentcontrolwidth=segmentcontrol_SearchType.frame.width;
I found something in other forums which should be a solution for objective C, but I don't understand this :-)
[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
any help ? (thanks :-) )
UPDATE
I tried the following, which doesn't work either. I go through all UILabels and set the numberOfLines = 0;
segmentcontrol_SearchType = UISegmentedControl(items: arraySearchTypes);
let font = UIFont.systemFontOfSize(fFontsize_SearchtypeValues)
segmentcontrol_SearchType.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
segmentcontrol_SearchType.frame.origin = CGPoint(x: spaceYElements, y: drawy);
segmentcontrol_SearchType.selectedSegmentIndex = saved_TextSizeTextViewer;
// Go through all sublabels and set wrapping
for labels in segmentcontrol_SearchType.subviews {
if labels.isKindOfClass(UILabel){
var myLabel:UILabel=labels as! UILabel;
myLabel.numberOfLines = 0;
}
}
segmentcontrol_SearchType.sizeToFit();

Related

Wrapping text on TitleLabel and DetailLabel - Material Card

I am using CosmicMind - Material Framework to create a Card. titleLabel and DetailLabel do not wrap to a new line.
My question is: What do I need to do to accomplish such task and is it supposed to be automatically adjusted by the framework?
This is what I have so far:
let card = Card()
var heartIcon = IconButton()
heartIcon = IconButton(image: Icon.favoriteBorder, tintColor: Color.red.base)
//Title Bar
let toolbar = Toolbar(rightViews: [heartIcon])
toolbar.title = cardData.cardTitleText
toolbar.titleLabel.textAlignment = .left
toolbar.titleLabel.numberOfLines = 0
toolbar.detail = "Company: " + cardData.cardTitleSubtitle
toolbar.detailLabel.font = RobotoFont.regular(with: 14)
toolbar.detailLabel.textColor = Color.grey.base
toolbar.detailLabel.textAlignment = .left
toolbar.detailLabel.numberOfLines = 0
And this is my output:
Update:
I managed to achieve what I wanted by increasing the size of the toolbar frame.
toolbar.frame = CGRect(x:0,y:0,width: view.frame.width,height: 100)
My goal was to at least show 2 lines of text.
Thanks!
I have no experience with the CosmicMind framework but usually, the label.numberOfLines property defines the maximum number of lines that the label text can have. You can set it to 2 instead of 0 and it should wrap.

How to force word wrapping of UILabel that adjusts font size and is multiline

The problem I am facing is that UILabel will break line in the middle of the word although I am using word wrapping.
You can create a new project and replace content of view controller to see the result:
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
label.center = CGPoint(x: view.frame.midX, y: view.bounds.midY)
label.numberOfLines = 2 // Setting this to 1 produces expected result
label.lineBreakMode = .byWordWrapping
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
view.addSubview(label)
label.text = "Singlewordtext"
label.backgroundColor = .red
}
This produces 2 lines of text which is broken in the middle of the word. The reason this naturally happens is because the word itself is wider than the label itself so it makes sense (I guess). But I would hope that it would use adjustsFontSizeToFitWidth and minimumScaleFactor prior to breaking it. If I set it to single line (label.numberOfLines = 1) I get expected result which is that the text will shrink instead of break. Note that doing so in this case will fit all of the text inside the label.
The question is, is there a configuration on UILabel to prevent line break in such case? Or is there some other elegant solution?
A current result:
Desired result (produced by using label.numberOfLines = 1):
Do note that I still do need to have 2 lines enabled to nicely display for instance label.text = "Three words fit".

How to navigate a page by using prev and next button like slide show for set of 10 ques?

I'm creating an online tutorial app. In that i want to conduct online test by using set of 10 questions.Could you suggest me in what way i have to design using previous and next button.My condition is,there should be a single Viewcontroller with multiple views(10),the view should change according to prev and next button in the tab bar.
Thanks for consideration.
You have to take all the 10 questions in 1 array & on click of prev next button change the index of array & show the contents in view which contain textview.There is no need of 10 views/Maintain only one view & change contents.Like on Next button increase the counter
i++;
NSString *new = [questionSequenceArray objectAtIndex:i]
questionText.text=new;
As #poojathorat, #Nilesh, #Akshay said you don't need so many viewcontrollers or even that many views. Just have a label and a couple of buttons and have your questions in an array. When the button is clicked change the label text accordingly.
To give you a head start,
// global variables
let qArray = ["Question 1", "Question 2", "Question 3", "Question 4", "Question 5"]
var currIndex = 0
var qLbl: UILabel!
This method will add respective label and buttons
func addQuestionView() {
let qView = UIView(frame: self.view.bounds)
qView.backgroundColor = UIColor.groupTableViewBackgroundColor()
qLbl = UILabel(frame: CGRect(x: 0, y: 0, width: qView.frame.size.width, height: qView.frame.size.height/2))
qLbl.text = qArray[currIndex]
qLbl.textAlignment = .Center
let prevBtn = UIButton(frame: CGRect(x: 0, y: qView.frame.size.height-50, width: qView.frame.size.width/2, height: 50))
prevBtn.tag = 15
prevBtn.setTitle("Prev", forState: .Normal)
prevBtn.backgroundColor = UIColor.darkGrayColor()
prevBtn.addTarget(self, action: "prevBtnAction", forControlEvents: UIControlEvents.TouchUpInside)
let nextBtn = UIButton(frame: CGRect(x: qView.frame.size.width/2, y: qView.frame.size.height-50, width: qView.frame.size.width/2, height: 50))
nextBtn.tag = 16
nextBtn.setTitle("Next", forState: .Normal)
nextBtn.backgroundColor = UIColor.darkGrayColor()
nextBtn.addTarget(self, action: "nextBtnAction", forControlEvents: UIControlEvents.TouchUpInside)
qView.addSubview(qLbl)
qView.addSubview(prevBtn)
qView.addSubview(nextBtn)
self.view.addSubview(qView)
}
And the buttons actions
func prevBtnAction() {
currIndex--
if currIndex >= 0 {
qLbl.text = qArray[currIndex]
} else {
currIndex++
}
}
func nextBtnAction() {
currIndex++
if currIndex < qArray.count {
qLbl.text = qArray[currIndex]
} else {
currIndex--
}
}
You can simplify even further but this is just for understanding. Hope it helps.
It depeds on your requirement, if each page contains single question and options no need of creating 10 different viecontrollers and you can use views efficiently,
If all questions are identical like just question and options you can use single reusable view. Unless you have each question will have different type of answers such as emojis, photo selection, etc.,
Better use reusable single view, move with animations and load appropriate data from objects. After completing all questions you can navigate to conclusion viewcontroller. My suggestion is not to create 10 unusable viewcontollers.
You can try following way,
Create one single view & array of your question then manage it using the previous/next button event in one controller. If you would like to display it with animation the need to some work on that.
You can even try to push the same controller every time with updated array index.

paged scrollView with labels

I'm trying to create a scrollView where u scroll between different labels. As an illustration i have this image:
I want to make the bottom scrollView i've tried creating a replicate, but in my case it is only adding "Book" to the scrollView and how can i make a smaller spacing between the labels. because in my code there are one label per self.view.frame.width
categoryArray = NSArray(objects: "Book", "Elektronik")
var textWidth = 0
for val in categoryArray!
{
var textLabel: UILabel = UILabel(frame: CGRectMake(0, CGFloat(textWidth), categoryScrollView!.frame.width, categoryScrollView!.frame.height))
textLabel.textAlignment = NSTextAlignment.Center
textLabel.text = val as NSString
categoryScrollView?.addSubview(textLabel)
textWidth = textWidth + Int(textLabel.frame.size.width)
if textWidth > Int(self.view.frame.width) {
categoryScrollView?.contentSize = CGSizeMake(CGFloat(textWidth), categoryScrollView!.frame.height);
}
}
I don't read swift very well, but it looks to me like the code is advancing the y position of the label frame on each iteration, rather than the x. In other words, change:
CGRectMake(0, CGFloat(textWidth), categoryScrollView!.frame.width, categoryScrollView!.frame.height)
to:
CGRectMake(CGFloat(textWidth), 0, categoryScrollView!.frame.width, categoryScrollView!.frame.height))

Adding UIScrollView to table cell seems to affect table scroll performance

I'm not sure this was poor implementation or a poor decision to start with, but I'm attempting to add a horizontal UIScrollView inside of each cell within my UITableView. I add it is as a subview to the cell's contentView, then call cell.clipsToBounds = true (FYI, I'm using RubyMotion). When I tap a button on the cell, I use beginUpdates/endUpdates to trigger heightForRowAtIndexPath to expand the cell's height and show the "hidden" scrollview.
Anyway, since doing that my table's scroll is a bit sluggish. I've added my code below and am wondering if my implementation can be improved to fix that scroll performance. Any help/insight is helpful (though if possible I'd like to try and keep the scroll views).
my_array = [
["Hi", "there", "one"],
["Hi", "there", "two"],
["Hi", "there", "three"],
["Hi", "there", "four"],
]
scroll_frame = CGRectMake(
0, rowHeight - 1, hidden_width, 51
)
hidden_scroll = UIScrollView.alloc.initWithFrame(scroll_frame)
hidden_scroll.alwaysBounceHorizontal = true
hidden_scroll.bounces = true
hidden_scroll.contentSize = CGSizeMake(hidden_width * my_array.size, scroll_frame.size.height)
hidden_scroll.delegate = parent
hidden_scroll.clipsToBounds = true
hidden_scroll.pagingEnabled = true
hidden_scroll.showsHorizontalScrollIndicator = false
hidden_scroll.layer.borderWidth = 1
hidden_scroll.layer.borderColor = UIColor.colorWithRed(200.0/255, green:200.0/255, blue:200.0/255, alpha: 1).CGColor
hidden_scroll.backgroundColor = UIColor.colorWithRed(248.0/255, green:248.0/255, blue:248.0/255, alpha: 1)
hidden_scroll.scrollEnabled = false
my_array.each_with_index do |data, index|
hidden_view = UIView.alloc.initWithFrame(CGRectMake(
(parent.view.frame.size.width * index), 0, parent.view.frame.size.width, 50
)
)
label_frame = CGRectMake(
40, 5, (parent.view.frame.size.width - 40 - 7) * 2/3, 40
)
label = UILabel.alloc.initWithFrame(label_frame)
label.font = UIFont.boldSystemFontOfSize(11)
label.backgroundColor = UIColor.clearColor
label.text = data[0]
label.numberOfLines = 0
label.lineBreakMode = UILineBreakModeWordWrap
label_size = data[0].sizeWithFont(label.font)
label.sizeToFit
new_frame = label.frame
new_frame.origin.y = (50 - label.frame.size.height) / 2
new_frame.size.height = label.frame.size.height
label.frame = new_frame
button = UIButton.buttonWithType(UIButtonTypeCustom)
button.backgroundColor = UIColor.orangeColor
button.font = UIFont.systemFontOfSize(11)
button.setTitle(data[3].upcase, forState:UIControlStateNormal)
button.setTitleColor(UIColor.whiteColor, forState:UIControlStateNormal)
button.layer.cornerRadius = 2.0
button.instance_variable_set(:#data, data)
button_size = data[3].upcase.sizeWithFont(button.font)
button.frame = [
[label.frame.origin.x + label_frame.size.width + 5, (50 - label_size.height) / 2 - 3],
[80, button_size.height + 6]
]
button.addTarget(parent, action:"add_user_goal:", forControlEvents:UIControlEventTouchUpInside)
hidden_view.addSubview(label)
hidden_view.addSubview(button)
hidden_scroll.insertSubview(hidden_view, belowSubview: cell.contentView)
end
Since the UIScrollView isn't in view anyway, try creating the UIScrollView only when the user taps the cell to view it. Before then, there's just an empty area of the cell.
For cleaner code, I'd also recommend subclassing UIScrollView and doing your customization in a custom initializer.
class CellScrollView < UIScrollView
def initWithCustom(args={})
self.initWithFrame(args[:frame])
# ... all your customizations. Use the args hash to pass in any other data you need.
self
end
end

Resources