Been going mad for days trying to fix this problem.
** Everytime a new line is created the textView jumps up.**
Inserting another character, after the line is created, adjusts the textView correctly to its frame.
So the issue is only when a new line is created.
It's a minor issue but its quite irritating.
func textViewDidChange(textView: UITextView) {
print("text view did change\n")
let fieldStatus = textView.text
let textViewFixedWidth: CGFloat = self.textView.frame.size.width
let newSize: CGSize = self.textView.sizeThatFits(CGSizeMake(textViewFixedWidth, CGFloat(MAXFLOAT)))
var newFrame: CGRect = self.textView.frame
var textViewYPosition = self.textView.frame.origin.y
var heightDifference = self.textView.frame.height - newSize.height
self.textView.frame = newFrame
self.textView.updateConstraintsIfNeeded()
self.UserMessageView.updateConstraintsIfNeeded()
self.UserMessageView.frame.height.floatingPointClass
UserMessageViewHeightConstraint.constant = newSize.height + 8
if UserMessageViewHeightConstraint.constant >= 250 {
println("stop growing")
let stopGrowth: CGFloat = self.UserMessageView.frame.height
let currentHeight = self.textView.frame.height
UserMessageViewHeightConstraint.constant = stopGrowth
}
if countElements(fieldStatus) >= 500 {
textView.deleteBackward()
}
println("\(countElements(fieldStatus))")
}
Has anyone encountered this before?
/Will
Related
there is a UIButton in view controller and when button clicked , UIButton move with random position in View Controller . The question it is how i can make disable movable ability when clicked again on button ?
this is moveable Method :
func randomPosition() {
self.movable_Button.layer.cornerRadius = 10
let buttonWidth = movable_Button.frame.width
let buttonHeight = movable_Button.frame.height
let viewWidth = movable_Button.superview!.bounds.width
let viewHeight = movable_Button.superview!.bounds.height
let xwidth = viewWidth - buttonWidth
let yheight = viewHeight - buttonHeight
let xPosition = CGFloat(arc4random_uniform(UInt32(xwidth)))
let yPosition = CGFloat(arc4random_uniform(UInt32(yheight)))
movable_Button.center.x = xPosition + buttonWidth / 2
movable_Button.center.y = yPosition + buttonHeight / 2
}
I think you have programmed something like
func onButtonClicked() {
randomPosition()
}
so you can make a new variable:
var clickedButton = false
func onButtonClicked() { // i know this is not the correct function, but i think you know what i mean ;)
if clickedButton == false {
randomPosition()
} else {
clickedButton = true
}
}
I am trying to update my score label at the end of the game. Since the variable (seems to be) outside of the scope I figured I would just remove the label and make a new one in the adjust function. I am not familiar with swift and would appreciate help. Can I just reposition or do I have to create new label? Cant figure it out. Thanks
func loadScore() {
let scoreBand = SKLabelNode(fontNamed: "Arial")
scoreBand.name = StickHeroGameSceneChildName.ScoreName.rawValue
scoreBand.text = "0"
scoreBand.position = CGPointMake(0, DefinedScreenHeight / 2 - 200)
scoreBand.fontColor = SKColor.whiteColor()
scoreBand.fontSize = 100
scoreBand.zPosition = StickHeroGameSceneZposition.ScoreZposition.rawValue
scoreBand.horizontalAlignmentMode = .Center
addChild(scoreBand)
}
func adjustScore() {
//var scoreBand = scoreBand
scoreBand.position = CGPointMake(0, DefinedScreenHeight / 2 - 100)//doesnt recognize scoreBand
}
You can reposition, just declare scoreBand as a variable of your containing class. For example:
class SomeClass: SKSpriteNode {
var scoreBand: SKLabelNode!
override init(size: CGSize) {
scoreBand = SKLabelNode(fontNamed: "Arial")
super.init(size: size)
anchorPoint = CGPointMake(0.5, 0.5)
physicsWorld.contactDelegate = self
}
func loadScore() {
scoreBand.name = StickHeroGameSceneChildName.ScoreName.rawValue
scoreBand.text = "0"
scoreBand.position = CGPointMake(0, DefinedScreenHeight / 2 - 200)
scoreBand.fontColor = SKColor.whiteColor()
scoreBand.fontSize = 100
scoreBand.zPosition = StickHeroGameSceneZposition.ScoreZposition.rawValue
scoreBand.horizontalAlignmentMode = .Center
addChild(scoreBand)
}
func adjustScore() {
scoreBand.position = CGPointMake(0, DefinedScreenHeight / 2 - 100)
}
}
So I am very new in Swift and I can't figure out how to increase the y value from a label.
I am trying to show in a label, some messages that a user types in an input... like a chat view.
So I have this code, but it puts all the messages one above the other:
I know that it is very messy, I am sorry for that.
Thanks!
//my label
#IBOutlet weak var label: UILabel!
//my text field
#IBOutlet weak var textF: UITextField!
#IBAction func buttonPressed(sender: AnyObject) {
//create a variable(input) to store my text from the TextField
var input = textF.text
//put that text in the label
label.text = input
var label2 : UILabel = UILabel(frame: CGRectMake(20, 20, 200, 21))
var isSendMessage: Bool = true {
didSet {
if isSendMessage {
var input = textF.text
label.text = input
textF.text = " "
// self.view.label.origin.y += 10
} else {
var input = textF.text
label2.text = input
textF.text = " "
}
}
}
label2.center = CGPointMake(160, 284)
label2.textAlignment = NSTextAlignment.Center
self.view.addSubview(label2)
//empty text field
textF.text = " "
label2.center = CGPointMake(160, 284)
label2.textAlignment = NSTextAlignment.Center
label2.text = input
//add it to the view
self.view.addSubview(label2)
}
Try this
Don't update Y axis directly as if your Y value is greater than the screen height then label will go off the screen.
Try the following code.
// Taking height and width of the screen
var height = UIScreen.mainScreen().bounds.size.height;
var width = UIScreen.mainScreen().bounds.size.width;
var x = 164.0 // Fixed height of the label
if x < height
{
x += 20.0 // Incrementing height value
if (height - 20) >= x // Checking the difference between screen height and x, so it doesn't go beyond the screen.
{
label.frame = CGRectMake(20.0, x, 200.0, 21.0)
self.view.addSubview(label)
}
else
{
// Setting default height
label.frame = CGRectMake(20.0,164.0, 200.0, 21.0)
self.view.addSubview(label)
}
}
Hope this will be helpful.
In a quick answer to your question, if you are just looking to change the position on the y-axis you can use the property .y under .center
self.yourLabel.center.y = self.yourLabel.center.y + 10
I found how to calculate dynamic uiwebview height according to its content. My code is :
println("webViweDidFinish started")
var frame:CGRect = myWebView.frame
frame.size.height = 1
var fittingSize :CGSize = myWebView.sizeThatFits(CGSizeZero)
frame.size = fittingSize
myWebView.frame = frame
var contentsizeHeight = webView.scrollView.contentSize.height
var yCoordinateOfWebView = myWebView.frame.origin.y
var contentHeight: CGFloat = webView.scrollView.contentSize.height;
myScrollView.contentSize = CGSizeMake(self.view.frame.width, yCoordinateOfWebView + contentsizeHeight + labelAuthorOfArticle.frame.height) //
UIView.animateWithDuration(0.5, animations: {
self.heightConstraints.constant = fittingSize.height
self.myWebView.layoutIfNeeded()
self.myScrollContentView.layoutIfNeeded()
})
self.activityIndicator.stopAnimating()
If I use this approach:
var height:NSString! = webView.stringByEvaluatingJavaScriptFromString("document.height;")
var heightInFloat = height.floatValue
How to convert height to CGFloat? I can only convert it to float as you see.
The problem is that when webview has too many images the height is calculated wrong. Oppositely when there is no image height is calculated right. When I reload the function, the second time it calculates right (with images). Here is content of string that I am loading:
class func formatContentText(inout text: NSString)-> NSString{
text = text.stringByReplacingOccurrencesOfString("src=\"", withString: "src=\"http://dixinews.kz")
var deviceWidth = "300"
text = "<html><head><meta name = \"viewport\" content = \"user-scalable=no, width=" + deviceWidth + " , maximum-scale=1.0\"><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body>" + text + "</body></html>"
return text
}
I used approach with javascript. Here is my code:
func webViewDidFinishLoad(webView: UIWebView) {
var frame:CGRect = myWebView.frame
// javascript
var height:NSString! = webView.stringByEvaluatingJavaScriptFromString("document.height;")
var heightInFloat = height.floatValue // convert to float
var heightINCGFloat = CGFloat(heightInFloat) convert to cgfloat
frame.size.height = heightINCGFloat //set heigh
myWebView.frame = frame // set frame
myWebView.scrollView.contentSize.height = myWebView.frame.height
var contentsizeHeight = webView.scrollView.contentSize.height
var yCoordinateOfWebView = myWebView.frame.origin.y
myScrollView.contentSize = CGSizeMake(self.view.frame.width, yCoordinateOfWebView + contentsizeHeight + labelAuthorOfArticle.frame.height) //
UIView.animateWithDuration(0.5, animations: {
self.heightConstraints.constant = heightINCGFloat
self.myWebView.layoutIfNeeded()
self.myScrollContentView.layoutIfNeeded()
})
self.activityIndicator.stopAnimating()
}
I've got a nice bar graph class working in an app using my own Core Graphics code, and playing around with it in playground I notice that drawRect seems to be called an awful lot of times when nothing is changing (I've abbreviated my code below, but theres the gist).
I've noticed if I put the axis drawing code in a method, then call that exactly where the code used to be, the playground runs drawRect thousands of times until it crashes the program. If I have it how it is shown below, it runs only once.
class BarGraphView: UIView {
var frameWidth = CGFloat(0.0)
var frameHeight = CGFloat(0.0)
var values = [CGFloat]()
var maxY = CGFloat(0.0)
var title = NSString(string: "")
var xLabel = NSString(string: "")
let gridOffSetY = CGFloat(0.1)
var xaxis : CGFloat {
return 0.6*frameWidth
}
var yaxis : CGFloat {
return 0.6*frameWidth
}
var xstart : CGFloat {
return 0.2*frameWidth
}
var ystart : CGFloat {
return 0.3*frameWidth
}
var xseparation: CGFloat {
return CGFloat(xaxis*0.05)
}
init(frame: CGRect, data: [CGFloat], maxYVal: Int, title: String, xLabel: String) {
super.init(frame: frame)
frameWidth = frame.width
frameHeight = frame.height
values = data
maxY = CGFloat(maxYVal)
self.title = title as NSString
self.xLabel = xLabel as NSString
}
override func drawRect(rect: CGRect) {
let numValues = CGFloat(values.count)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor);
CGContextFillRect(context, bounds);
CGContextSetFillColorWithColor(context, UIColor.greenColor().CGColor)
let barWidth:CGFloat = ((xaxis-(numValues+1)*xseparation)/numValues)
var count:CGFloat = 0
for number in values {
let x = xstart + xseparation + (count * (barWidth + xseparation))
let barRect = CGRect(x: x, y:ystart+yaxis, width: barWidth, height: -(number/maxY)*yaxis)
CGContextAddRect(context, barRect)
count++
}
CGContextFillPath(context)
// IF THIS CODE IS HERE, IT IS RUN ONCE IN PLAYGROUND
// Axis Drawing
var bezier = UIBezierPath()
// Draw y-axis
bezier.moveToPoint(CGPointMake(xstart,ystart))
bezier.addLineToPoint(CGPointMake(xstart,ystart+yaxis+5))
// Draw x-axis
bezier.moveToPoint(CGPointMake(xstart-5,ystart+yaxis))
bezier.addLineToPoint(CGPointMake(xstart+xaxis,ystart+yaxis))
// Draw value markings on y-axis
let divisor = CGFloat((yaxis-gridOffSetY)/maxY) // max y-value, with offset
for i in 1..<maxY {
bezier.moveToPoint(CGPointMake(xstart-CGFloat(2.5),ystart+gridOffSetY+CGFloat(i)*divisor))
bezier.addLineToPoint(CGPointMake(xstart,ystart+gridOffSetY+CGFloat(i)*divisor))
}
UIColor.blackColor().setStroke()
bezier.stroke()
// IF PUT THAT ^ in this method, it runs thousands of times consecutively, crashing Xcode.
//drawAxis()
}
Does this look like potentially buggy behaviour, is playground just being weird, or is there any logical explanation for this?